Jump to content

PHP generate form from data, return to calling script issue


Sonzai

Recommended Posts

Hello, I am hoping someone can help with this issue and even if not, clarify some questions I have been having.

 

The two code files I am showing below are a simple mock-up of a larger project using the same concept.

 ***name.php***
<?php
echo "some crap here";

include ('simple.php');

if(isset($_REQUEST['firstname']))
{
$firstName = $_REQUEST['firstname'];
$lastName = $_REQUEST['lastname'];

echo 'Welcome to our website, ' .
htmlspecialchars($firstName, ENT_QUOTES, 'UTF-8') . ' ' .
htmlspecialchars($lastName, ENT_QUOTES, 'UTF-8') . '!';

}
?>

***simple.php***
<html>
<head></head>
<body>
<form name="simple" method="post" action="name.php">
<?php echo "please tell me your first and last name"; ?>
<input type="text" name="firstname" id="firstname" form="simple">
<input type="text" name="lastname" id="lastname" form="simple">
<input type="submit" value="submit" form="simple">
</form>
</body>
</html>

 

the idea is that I need to use some variables created before the include in generating the form in "simple.php" ...I know that is not really the case in this example but in the larger project I mentioned, it is. So I include the file to show the form and all seems well however the following problems occur 1) no data is picked up by $_REQUEST...(that I can see, I am imagining that after the include the script does not wait for the form return), and 2) though the action field in the form directs it to "name.php", it neither re-runs the script nor returns to the point at which it was included. I will try to reply to this with a more similar example if what I'm trying to do is unclear.

Link to comment
Share on other sites

This is the less simple, but more similar example of what I am doing to help those who would like a better example.

***phpfrom.php***
<?php

  $test1d = array("Name"=>NULL, "Age"=>NULL, "Sign"=>NULL);
  $test2d = array();

  $test1d['Name'] = 'Haru';
  $test1d['Age']  = NULL;
  $test1d['Sign'] = 'Pisces';

  $test2d[] = $test1d;

  $test1d['Name'] = 'Shizuku';
  $test1d['Age']  = 15;
  $test1d['Sign'] = NULL;

  $test2d[] = $test1d;

  $test1d['Name'] = 'Yamaken';
  $test1d['Age']  = NULL;
  $test1d['Sign'] = NULL;

  $test2d[] = $test1d;

  include ('phpto.php');

   if(isset($_REQUEST['test']))
  {  echo " the rest is gone";
  $updates = $_REQUEST['Updates[]'];
  $nums = count($updates) -1;
  echo "number in updates[] ".$nums;
 if($nums >= 0)
 {
 $test2d[$nums]['Sign'] = 'Ophiuchus'; echo $test2d[$nums]['Name']."is now an Ophiuchus"; 
 }

 }


?>

 

includes this:

***phpto.php***
<html>
<head></head>
<body>
<form method="POST" action="phpfrom.php" name="corrections">
 <?php
  echo " <table cellpadding=5 >
	   <tr><td><h3>Name</h3></td>
  <td><h3>Age</h3></td>
  <td><h3>Sign</h3></td>
  <td><h3>Update</h3></td>
 </tr>";

   for($increm = 0; $increm < count($test2d); $increm++)
 {
    echo "<tr><td>".$test2d[$increm]['Name']."</td>";

 if($test2d[$increm]['Age'] == NULL)
   { echo "<td><input type=\"text\" name=\"Ages[".$increm."]\" form=\"corrections\"></td>"; }
 else
   { echo "<td>".$test2d[$increm]['Age']."</td>" ;}

 if($test2d[$increm]['Sign'] == NULL)
   { echo "<td><input type=\"text\" name=\"Signs[".$increm."]\" form=\"corrections\"></td>"; }
 else
   { echo "<td>".$test2d[$increm]['Sign']."</td>";}

 echo '<td><input type="checkbox" name="Updates[]"
			   value='.$increm.' form="corrections"></td></tr>';

 } 
   echo "</table>";
?>
   <input type="hidden" name="test" form="corrections">
<input type="submit" value="submit" form="corrections"/>

</form>
</body>
</html>

Link to comment
Share on other sites

Not sure what you mean to do with the data when the form is posted - so I didn't touch that part of your code, but this should get you on the right path:

 

phpfrom.php

<?php

   $dataAry = array();

   $dataAry[] = array(
    'Name' => 'Haru',
    'Age'  => NULL,
    'Sign' => 'Pisces'
    );
   $dataAry[] = array(
    'Name' => 'Shizuku',
    'Age'  => 15,
    'Sign' => NULL
    );
   $dataAry[] = array(
    'Name' => 'Yamaken',
    'Age'  => NULL,
    'Sign' => NULL
    );

   include ('phpto.php');

   if($_SERVER['REQUEST_METHOD'] == 'POST')
   {
    echo " the rest is gone<br>\n";
    $updates = $_POST['Updates'];
    $nums = count($updates);
    echo "number in updates[] " . $nums . "<br>\n";
    if($nums >= 0)
    {
	    $dataAry[$nums]['Sign'] = 'Ophiuchus ';
	    echo $dataAry[$nums]['Name'] . " is now an Ophiuchus";
    }

   }

?>

 

phpto.php

<html>
<head></head>
<body>
<form method="POST" action="" name="corrections">

<table cellpadding="5" border="1" >
 <tr><td><h3>Name</h3></td>
   <td><h3>Age</h3></td>
   <td><h3>Sign</h3></td>
   <td><h3>Update</h3></td>
 </tr>

<?php

   foreach($dataAry as $index => $data)
   {
    echo "  <tr>\n";
    echo "    <tr><td>{$data['Name']}</td>\n";

    if($data['Age'] == NULL)
    {
	    echo "    <td><input type=\"text\" name=\"Ages[{$index}]\"></td>\n";
    }
    else
    {
	    echo "    <td>{$data['Age']}</td>\n" ;
    }

    if($data['Sign'] == NULL)
    {
	    echo "    <td><input type=\"text\" name=\"Signs[{$index}]\"></td>\n";
    }
    else
    {
	    echo "    <td>{$data['Sign']}</td>\n";
    }

    echo "    <td><input type=\"checkbox\" name=\"Updates[]\" value=\"{$index}\"></td>\n";
    echo "  <\tr>\n";
   }

?>
</table>

<input type="hidden" name="test">
<input type="submit" value="submit"/>

</form>
</body>
</html>

Edited by Psycho
Link to comment
Share on other sites

Thank you very much for the effort, looking at your code has informed me of new "clearer" ways of doing things.

 

In my hurry to get an example up, I left several errors so they may be the source of the problem, but here is the major irritation:

Whenever I change the script so that it no longer is picked up by the parent, rather let's say the form posts to "phpto2.php" all I get is a print out of the code contained in that script.

When the action returns to the calling script "phpfrom.php", the original form is still visible.

 

Also, I believe this line is what caused the code to run correctly, but if there are multiple forms inputtting into the phpfrom.php script won't this be true for

any number of them and not specifically phptp.php? $_SERVER['REQUEST_METHOD'] == 'POST'

 

Lastly, form is an acceptable attribute for input types, however it references the form id rather than the name I believe, so I slipped up there

Link to comment
Share on other sites

Whenever I change the script so that it no longer is picked up by the parent, rather let's say the form posts to "phpto2.php" all I get is a print out of the code contained in that script.

When the action returns to the calling script "phpfrom.php", the original form is still visible.

Hard to say. If the file being called has a php extension and it is properly constructed with opening and closing PHP tags <?php ?> then it should be parsed by the PHP engine.

 

Also, I believe this line is what caused the code to run correctly, but if there are multiple forms inputtting into the phpfrom.php script won't this be true for

any number of them and not specifically phptp.php? $_SERVER['REQUEST_METHOD'] == 'POST'

That isn't what made it run correctly, but that is a better way to know if a form was POSTed. If you have multiple forms that are posted, then you would do another check to know which one. I would typically use a hidden form field with a set value for that.

 

Lastly, form is an acceptable attribute for input types, however it references the form id rather than the name I believe, so I slipped up there

So it is. I hadn't seen that change yet. But, not sure why you needed it in this case. It's not even fully supported yet (OK, on not supported in IE apparently, but still). based upon your layout, you can include all the fields in the FORM opening and closing tags, so I see no reason to add complexity.

Link to comment
Share on other sites

  I thought this important to post:

 

      This line echo ' <td><input type="checkbox" name="Updates[]" value="{$index}"></td> ';

              is intended to use the index as the value  of the update, however in this form "{index}" is being passed as the value

              this format is needed to pass the value of the the index as a string

 

                    echo ' <td><input type="checkbox" name="Updates[]" value=" '. $index .' "></td> '

Link to comment
Share on other sites

You didn't pay attention to what I posted. I did not provide this:

echo ' <td><input type="checkbox" name="Updates[]" value="{$index}"></td> ';

 

I provided this

echo "    <td><input type=\"checkbox\" name=\"Updates[]\" value=\"{$index}\"></td>\n";

 

Which will result in the exact same result as

echo ' <td><input type="checkbox" name="Updates[]" value=" '. $index .' "></td> ';

 

Note the double quotes I used to define the string vs. the single quotes you used. That makes all the difference. If you are going to try and correct someone, be sure you are right.

 

From the manual:

http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing

 

When a string is specified in double quotes or with heredoc, variables are parsed within it.

emphasis added

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.