Jump to content


Photo

PHP form resetting


  • Please log in to reply
4 replies to this topic

#1 KDragon

KDragon
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 13 April 2006 - 04:45 AM

I really have no idea why in the following script, all the fields in the form reset after I submit. I've looked at about 20 tutorials about this and they all say to do the same thing that I believe I'm doing...that is to make the value attribute of the various fields = to <?php echo $_POST['varname'] ?>. For some reason it doesn't work at all. To test it I've echoed one of the variables (the $client_email one) several times along the running of the script to see if it was being unset anywhere but it came back with the string it was set to every time, except in the form which is where I want it. Can anybody please help?

<?php
        function showForm(){ ?>
        <form method="post" action="<?PHP echo $_SERVER['PHP_SELF']; ?>">
        <div>
        <font class="formtitle">Choose a type of avatar:</font>
        <br>
        <input type="radio" name="avatar_type" value="normal ">
        Normal
        <br>
        <input type="radio" name="avatar_type" value="animated">
        Animated
        <br>
        <br>
        <font class="formtitle">Text desired on avatar:</font>
        <input type="text" size="50" name="avatar_text" value="<?php echo $avatar_text ?>">
        <br>
        <br>
        <font class="formtitle">Desired dimensions (not required):</font>
        <input type="text" size="25" name="desired_dimensions" value="<?php echo $desired_dimensions; ?>">
        </div>
         
        Remember that a picture is worth a 1000 words and accordingly one should give a detailed description of what is desired in the following Description Area:
          <br>
          <textarea name="description_area" rows="10" cols="70"><?php echo $description_area; ?></textarea>
          <br>
          <br>
          Your email:  
          <input type="text" size="40" name="client_email" value="<?php $client_email; ?>">
          <br>
          <br>
          <input type="submit" name="submit" value="Send Request">
          </div>
          </form>
         <?php }
        ?> 
        <?php
        function showThankYou(){
          echo 'Thank you for your request. An email asking you to confirm your request
                 should be sent to you shortly.';
        }
        ?>
        
        <?php
        function checkEmail($email){

       $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';

       $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';

       $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
           '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';

       $quoted_pair = '\\x5c\\x00-\\x7f';

       $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";

       $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";

       $domain_ref = $atom;

       $sub_domain = "($domain_ref|$domain_literal)";

       $word = "($atom|$quoted_string)";

       $domain = "$sub_domain(\\x2e$sub_domain)*";

       $local_part = "$word(\\x2e$word)*";

       $addr_spec = "$local_part\\x40$domain";

       return preg_match("!^$addr_spec$!", $email) ? 1 : 0;
   }

?>

      <?php //declaring variables
        $avatar_type=$_POST["avatar_type"];
        $avatar_text=$_POST["avatar_text"];
        $desired_dimensions=$_POST["desired_dimensions"];
        $description_area=$_POST["description_area"];
        $client_email=$_POST["client_email"];
        $submit=$_POST["submit"];
        $category='avatar';
        $checker=false;
        $subject="Request Confirmation";
        $message="<h1>HTML E-mail</h1>
                  <p>This is an <b>HTML</b> e-mail.</p>";
        $headers = "MIME-Version: 1.0\r\n";
        $headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
        $headers.= "From: $from\r\n";
        $from="FROM:  <asukafan85@yahoo.com>";
?>

      <?php
      if(isset($submit)){
        if(empty($avatar_type)){
          echo 'Please select a type of avatar.'; ?>
          showForm();<?php
          unset($submit);
          $checker=true;
          }
        //check email
        if(checkEmail($client_email)==0){
          echo 'Please enter a valid email address.';
          $checker=true;
        }
        //shows thank you message if everything is ok
        if(!$checker){
          showThankYou();
          mail($client_email,$subject,$message,$headers);
        }
      }
      else{
        //display form
        showForm(); 
        }
      ?>


#2 bonaparte

bonaparte
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts
  • Location#172, I Floor, 'Hamsashree', Defence Colony, Sahakaranagar, Bangalore, India

Posted 13 April 2006 - 06:18 AM

[!--quoteo(post=364277:date=Apr 12 2006, 11:45 PM:name=KDragon)--][div class=\'quotetop\']QUOTE(KDragon @ Apr 12 2006, 11:45 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I really have no idea why in the following script, all the fields in the form reset after I submit. I've looked at about 20 tutorials about this and they all say to do the same thing that I believe I'm doing...that is to make the value attribute of the various fields = to <?php echo $_POST['varname'] ?>. For some reason it doesn't work at all. To test it I've echoed one of the variables (the $client_email one) several times along the running of the script to see if it was being unset anywhere but it came back with the string it was set to every time, except in the form which is where I want it. Can anybody please help?

<?php
        function showForm(){ ?>
        <form method="post" action="<?PHP echo $_SERVER['PHP_SELF']; ?>">
        <div>
        <font class="formtitle">Choose a type of avatar:</font>
        <br>
        <input type="radio" name="avatar_type" value="normal ">
        Normal
        <br>
        <input type="radio" name="avatar_type" value="animated">
        Animated
        <br>
        <br>
        <font class="formtitle">Text desired on avatar:</font>
        <input type="text" size="50" name="avatar_text" value="<?php echo $avatar_text ?>">
        <br>
        <br>
        <font class="formtitle">Desired dimensions (not required):</font>
        <input type="text" size="25" name="desired_dimensions" value="<?php echo $desired_dimensions; ?>">
        </div>
         
        Remember that a picture is worth a 1000 words and accordingly one should give a detailed description of what is desired in the following Description Area:
          <br>
          <textarea name="description_area" rows="10" cols="70"><?php echo $description_area; ?></textarea>
          <br>
          <br>
          Your email:  
          <input type="text" size="40" name="client_email" value="<?php $client_email; ?>">
          <br>
          <br>
          <input type="submit" name="submit" value="Send Request">
          </div>
          </form>
         <?php }
        ?> 
        <?php
        function showThankYou(){
          echo 'Thank you for your request. An email asking you to confirm your request
                 should be sent to you shortly.';
        }
        ?>
        
        <?php
        function checkEmail($email){

       $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';

       $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';

       $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
           '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';

       $quoted_pair = '\\x5c\\x00-\\x7f';

       $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";

       $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";

       $domain_ref = $atom;

       $sub_domain = "($domain_ref|$domain_literal)";

       $word = "($atom|$quoted_string)";

       $domain = "$sub_domain(\\x2e$sub_domain)*";

       $local_part = "$word(\\x2e$word)*";

       $addr_spec = "$local_part\\x40$domain";

       return preg_match("!^$addr_spec$!", $email) ? 1 : 0;
   }

?>

      <?php //declaring variables
        $avatar_type=$_POST["avatar_type"];
        $avatar_text=$_POST["avatar_text"];
        $desired_dimensions=$_POST["desired_dimensions"];
        $description_area=$_POST["description_area"];
        $client_email=$_POST["client_email"];
        $submit=$_POST["submit"];
        $category='avatar';
        $checker=false;
        $subject="Request Confirmation";
        $message="<h1>HTML E-mail</h1>
                  <p>This is an <b>HTML</b> e-mail.</p>";
        $headers = "MIME-Version: 1.0\r\n";
        $headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
        $headers.= "From: $from\r\n";
        $from="FROM:  <asukafan85@yahoo.com>";
?>

      <?php
      if(isset($submit)){
        if(empty($avatar_type)){
          echo 'Please select a type of avatar.'; ?>
          showForm();<?php
          unset($submit);
          $checker=true;
          }
        //check email
        if(checkEmail($client_email)==0){
          echo 'Please enter a valid email address.';
          $checker=true;
        }
        //shows thank you message if everything is ok
        if(!$checker){
          showThankYou();
          mail($client_email,$subject,$message,$headers);
        }
      }
      else{
        //display form
        showForm(); 
        }
      ?>
[/quote]

Try this:

<?php
function showForm(){
extract($_POST);
?>
<form method="post" action="<?PHP echo $_SERVER['PHP_SELF']; ?>">
...........
...........
.........


The problem is with the scope of the variables.

[a href=\"http://in2.php.net/manual/en/language.variables.scope.php\" target=\"_blank\"]http://in2.php.net/manual/en/language.variables.scope.php[/a]




#3 KDragon

KDragon
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 13 April 2006 - 06:41 AM

[!--quoteo(post=364298:date=Apr 13 2006, 02:18 AM:name=bonaparte)--][div class=\'quotetop\']QUOTE(bonaparte @ Apr 13 2006, 02:18 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Try this:

<?php
function showForm(){
extract($_POST);
?>
<form method="post" action="<?PHP echo $_SERVER['PHP_SELF']; ?>">
...........
...........
.........
The problem is with the scope of the variables.

[a href=\"http://in2.php.net/manual/en/language.variables.scope.php\" target=\"_blank\"]http://in2.php.net/manual/en/language.variables.scope.php[/a]
[/quote]
I tried that and it didn't work. I'm not totally sure but I don't think scope is the problem because POST is a super global.

#4 BladeMetal

BladeMetal
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 13 April 2006 - 07:52 AM

sure, $_POST is a super global, but it is used to access values from ONE page to another. You can't then go to a second page and expect the $_POST variables to stil work. The scope of $_POST is for the page referenced to in <form action="page.php"> meaning that only page.php can access the $_POST array values from the form.

#5 KDragon

KDragon
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 13 April 2006 - 01:24 PM

[!--quoteo(post=364318:date=Apr 13 2006, 03:52 AM:name=BladeMetal)--][div class=\'quotetop\']QUOTE(BladeMetal @ Apr 13 2006, 03:52 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
sure, $_POST is a super global, but it is used to access values from ONE page to another. You can't then go to a second page and expect the $_POST variables to stil work. The scope of $_POST is for the page referenced to in <form action="page.php"> meaning that only page.php can access the $_POST array values from the form.
[/quote]
Yeah, I have my form referencing the current page for it's action. So why would scope be a problem?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users