PHP/Form/Form Data

Материал из Web эксперт
Версия от 07:04, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Accessing multiple submitted values

 
<form method="POST" action="index.php">
<select name="lunch[]" multiple>
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
<option value="d">D</option>
<option value="e">E</option>
</select>
<input type="submit" name="submit">
</form>
Selected buns:
<br/>
<?php
foreach ($_POST["lunch"] as $choice) {
    print "You want a $choice bun. <br/>";
}
?>



Access widget"s form value

 
<INPUT TYPE="text" NAME="myform.email">

would be accessed in PHP as the following:
<?php
    echo $_GET["myform_email"]; 
?>



A process_form() Function

 
<?php
    function process_form($data) {
        $msg = "The form at {$_SERVER["PHP_SELF"]}
                was submitted with these values: \n\n";
        foreach($data as $key=>$val) {
            $msg .= "$key => $val\n";
        }
        mail("joeuser@somewhere.ru", "form submission", $msg);
    }
?>



Calculation based on form data

 
<html>
 <head> 
  <title>Calculation Form</title> 
 </head>
 <body>
  <form action = "calc.php" method = "post">
  Value 1: <input type = "text" name = "val1" size = "10">
  Value 2: <input type = "text" name = "val2" size = "10">
  <br>
  Calculation: <br>
  <input type = "radio" name = "calc" value = "add"> Add
  <input type = "radio" name = "calc" value = "sub"> Subtract
  <input type = "radio" name = "calc" value = "mul"> Multiply
  <input type = "radio" name = "calc" value = "div"> Divide 
  <hr>
  <input type = "submit" value = "Calculate"> 
  <input type = "reset" value = "Clear">
  </form>
 </body>
</html>

File: calc.php


<html> 
 <head> 
  <title>Calculation Result</title> 
 </head>
 <body>
 <?php 
  $val1 = $_POST["val1"];
  $val2 = $_POST["val2"];
  $calc = $_POST["calc"];
  if( is_numeric( $val1 ) && is_numeric( $val2 ) )
  {
      if( $calc != null )
      {
          switch( $calc )
          {
              case "add" : $result = $val1 + $val2; break;
              case "sub" : $result = $val1 - $val2; break;
              case "mul" : $result = $val1 * $val2; break;
              case "div" : $result = $val1 / $val2; break;
          }      
        echo( "Calculation result: $result" );
      } 
  }
  else
  { 
    echo( "Invalid entry - please retry" ); 
  }
 ?>
 </body>
</html>



Deal with Array Form Data

<HTML>
<BODY>
<FORM METHOD="POST" ACTION="DealWithArrayFormData.php">
<H1>Contact Information</H1>
<TABLE>
<TR>
  <TD>Childrens" Names:</TD>
</TR>
<TR>
  <TD><INPUT TYPE="TEXT" NAME="children[]"></TD>
</TR>
<TR>
  <TD><INPUT TYPE="TEXT" NAME="children[]"></TD>
</TR>
<TR>
  <TD><INPUT TYPE="TEXT" NAME="children[]"></TD>
</TR>
<TR>
  <TD><INPUT TYPE="TEXT" NAME="children[]"></TD>
</TR>
<TR>
  <TD><INPUT TYPE="TEXT" NAME="children[]"></TD>
</TR>
</TABLE>
<BR>
<BR>
<BR>
<INPUT TYPE="SUBMIT" VALUE="Submit">
<BR>
<BR>
<INPUT TYPE="RESET"  VALUE="Clear the Form">
</FORM>
</BODY>
</HTML>
<!-- DealWithArrayFormData.php
<?php
foreach ($children as $index => $child){
    echo "<BR>child[$index]=$child";
}
echo "<BR>";
sort($children);
foreach ($children as $index => $child){
    echo "<BR>child[$index]=$child";
}
?>
-->



Feedback Form

 
<html>
 <head>
  <title>Feedback Form</title>
 </head>
 <body>
 <form action="feedback.php" method="post">
 Name:<input type="text" name="username" size="30">
 <br><br>
 Email:<input type="text" name="useraddr" size="30">
 <br><br>
 <textarea name="comments" cols="30" rows="5">
 </textarea><br>
 <input type="submit" value="Send Form">
 </form>
 </body>
</html>

File: feedback.php

<?php
$username = $_POST["username"];
$useraddr = $_POST["useraddr"];
$comments = $_POST["comments"];
$to = "php@h.ru";  
$re = "Website Feedback";         
$msg = $comments;            
$headers  = "MIME-Version: 1.0\r\n";  
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: $useraddr \r\n";    
$headers .= "Cc: another@hotmail.ru \r\n";
mail( $to, $re, $msg, $headers );      
?>
<html>
 <head>
  <title>Message Received</title>
 </head>
 <body>
 <h3>Thanks for your comments</h3>
 Message received from <?php echo($username); ?><br>
 Reply to <?php echo($useraddr); ?>
 </body>
</html>



Forms and PHP

 
//index.htm
<html>
<body>
<form action="index.php" method="post">
Your Name:<br>
<input type="text" name="name" size="20" maxlength="20" value=""><br>
Your Email:<br>
<input type="text" name="email" size="20" maxlength="40" value=""><br>
<input type="submit" value="go!">
</form>
</body>
</html>

//index.php
<html>
<head>
<title></title>
</head>
<body>
<?
    print "Hi, $name!. Your email address is $email";
?>
</body>
</html>



Handling Special Characters

 
<html>
<body>
    <?php
      if ($_POST["submitted"] == "yes"){
        $yourname = $_POST["yourname"];
        $yourname = trim ($yourname);
        $yourname = strip_tags ($yourname);
        $yourname = htmlspecialchars ($yourname);
        $yourname = addslashes ($yourname);
        
        echo $yourname . "<br />";
        ?><a href="index.php">Try Again</a><?php
      }
      if ($_POST["submitted"] != "yes"){
        ?>
        <form action="index.php" method="post">
          <p>Example:</p>
          <input type="hidden" name="submitted" value="yes" />
          Your Name: <input type="text" name="yourname" maxlength="150" /><br />
          <input type="submit" value="Submit" style="margin-top: 10px;" />
        </form>
        <?php
      }
    ?>
  </div>
</body>
</html>



multivalue-forms.php

 
<?php
   if (isset($_POST["submit"]))
   {
      echo "You like the following languages:<br />";
      foreach($_POST["languages"] AS $language) echo "$language<br />";
   }
?>
<form action="index.php" method="post">
   Check all that apply): 
   <input type="checkbox" name="languages[]" value="csharp" />C#<br />
   <input type="checkbox" name="languages[]" value="jscript" />JavaScript<br />
   <input type="checkbox" name="languages[]" value="perl" />Perl<br />
   <input type="checkbox" name="languages[]" value="php" />PHP<br />
   <input type="submit" name="submit" value="Go!" />
</form>



Saying "Hello"

 
<?
if (array_key_exists("my_name",$_POST)) {
    print "Hello, ". $_POST["my_name"];
} else {
    print<<<_HTML_
<form method="post" action="$_SERVER[PHP_SELF]">
 Your name: <input type="text" name="my_name">
<br/>
<input type="submit" value="Say Hello">
</form>
_HTML_;
}
?>