www.webdeveloper.com
Results 1 to 4 of 4

Thread: Explanation needed...

  1. #1
    Join Date
    Jan 2011
    Posts
    79

    Explanation needed...

    Hi guys,

    Can anyone help me to explain here, I found this code online and need some inputs.

    1. What is the purpose of the foreach use here for?
    2. Why we are using onload javascript here? what is the objective?

    PHP Code:
    <?php
    ..
    [
    validation firstnamesurnameaddress etc]
    ..
      
    $input_field "";
          foreach (
    $_POST as $key => $val
          {
             
             if (
    $key != "submit"
             {
                 
    $input_field .= "<input type='hidden' name = '$key' value = '$val' />";
             }
          }
    ..
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Create Confirm</title>
    </head>

    <body onload='javascript:document.createConfirmation.submit();'>

    <form name="createConfirmation" action="confirmCreate.php" method="post">
     <?php echo $input_field;?>
    </form>

    Submitting...
    </body>
    </html>
    <?php
        
    exit;    
        }        
    }
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    </head>

    <body>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

    <div>
      <div align="center">
      <table width="300" border="1">
      
        <tr>
          <th><strong>First Name * </strong></th>
            <td><input type="text" name="firstname" value="<?php echo $firstname;?>" /> </td>
          </tr>
    ...
    ...

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,536
    The foreach loop is creating an hidden form field for each element found in the $_POST array. The onload call is submitting the form into which those form fields are inserted within the HTML <form> section. Why you would want to do it that way is unknown to me.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jan 2011
    Posts
    79
    Thanks NogDog,

    Well, the reason I want to do the "submitting" form is because to check if there are values been sending into the form - I saw some people use that method for checking purposed? Is that not correct?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,536
    I don't know, but it seems to me that as soon as the page finished loading it would then submit the form, which would take you to the form-handler page that is that form field's action, and without the user ever doing anything, so it seems like wasted HTTP traffic between the server and browser. Normally, if the validation is successful, I would have just run the processing code at that point, which you could probably do simply by including your form-handler script at that point instead of going through this extra stuff:
    PHP Code:
    <?php
    // do validation stuff, then do...
    if($validation_is_ok) { // change to however you confirm inputs are OK
       
    require 'confirmCreate.php';
       exit;
    }
    // rest of page, without that foreach loop, the onload stuff or the hidden form
    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    </head>

    <body>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
    etc....
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles