Home >> FAQs/Tutorials >> PHP Script Tutorials and Tips >> Index

PHP Script Tips - Processing Web Forms

By: FYICenter.com

Part:   1  2  3  4  5  6  7   8 

(Continued from previous part...)

How To Submit Values without a Form?

If you know the values you want to submit, you can code the values in a hyper link at the end of the URL. The additional values provided at the end of a URL is called query string. There are two suggestions on how to use query strings to submit values to the receiving script page:

  • Code values in the same way as the form method GET as: url?name=value&name=value... This allows the receiving script to retrieve input values in the $_REQUEST array or $_GET array.
  • Code values in your own wan as: url?your_values_in_your_format. The receiving script needs to pick up the input values in $_SERVER['QUERY_STRING'].

Here is a simple script page that shows you two hyper links with query strings in different formats:

<?php
  print("<html>");
  print("<p>Please click the links below"
    ." to submit comments about FYICenter.com:</p>");
  print("<p>"
    .'<a href="processing_forms.php?name=Guest&comment=Excellent">'
    ."It's an excellent site!</a></p>");
  print("<p>"
    .'<a href="processing_forms.php?Visitor,Average">'
    ."It's an average site.</a></p>");
  print("</html>");
?>

If you copy this script as submit_comments.php to your Web server, and click the first link, you will get:

Number of values: 2
  name = Guest
  comment = Excellent

If you click the second link, the current processing_forms.php will not pick up input values from $_REQUEST properly as showb below:

Number of values: 1
  Visitor,Average = 

How To Retrieve the Original Query String?

If you have coded some values in the URL without using the standard form GET format, you need to retrieve those values in the original query string in $_SERVER['QUERY_STRING']. The script below is an enhanced version of processing_forms.php which print the original query string:

<?php
  print("<html><pre>");
  print("  query_string = {$_SERVER['QUERY_STRING']}\n");
  $count = count($_REQUEST);
  print("Number of values: $count\n");
  foreach ($_REQUEST as $key=>$value) {
    if (is_array($value)) {
      print("  $key is an array\n");
      for ($i = 0; $i < count($value); $i++) {
         $sub_value = $value[$i];
         if (get_magic_quotes_gpc()) {
           $sub_value = stripslashes($sub_value);
         }
         print("    ".$key."[".$i."] = ".$sub_value."\n");
      }
    } else { 
      if (get_magic_quotes_gpc()) {
        $value = stripslashes($value);
      }
      print("  $key = $value\n");
    }
  }
  print("</pre></html>\n");
?>

How To Protect Special Characters in Query String?

If you want to include special characters like spaces in the query string, you need to protect them by applying the urlencode() translation function. The script below shows how to use urlencode():

<?php
  print("<html>");
  print("<p>Please click the links below"
    ." to submit comments about FYICenter.com:</p>");
  $comment = 'I want to say: "It\'s a good site! :->"';
  $comment = urlencode($comment);
  print("<p>"
    ."<a href=\"processing_forms.php?name=Guest&comment=$comment\">"
    ."It's an excellent site!</a></p>");
  $comment = 'This visitor said: "It\'s an average site! :-("';
  $comment = urlencode($comment);
  print("<p>"
    .'<a href="processing_forms.php?'.$comment.'">'
    ."It's an average site.</a></p>");
  print("</html>");
?>

(Continued on next part...)

Part:   1  2  3  4  5  6  7   8 


Selected Developer Jobs:

More...