Page 1 of 1

load php script within php

Posted: Sun Oct 02, 2005 11:51 pm
by kristolklp
I have an HTML form that has input fields and a submit button. Upon submission I would like to first check that all fields have been filled in, and if so, the load the next php file. The problem is that I have a function setup to check my fields after submission so I need to set my form's action to my first php page. If all fields are filled in, then I need to pass variables to my next script. How can I this be done through php (or javascript)?

here is my current php code:

Code: Select all

<html>
<head>
<title>Place Order</title>
</head>
<body>

<?php function show_form($first="",$last="") {  ?>
<form method="post" action="THISPAGE.php" enctype="multipart/form-data">
  <div align="left"><strong>PLACE ORDER</strong></div>
      <table>
      <tr> 
        <th>First Name</th>
        <th> 
            <input type="text" name="first" value="<?php echo $last ?>"  size="20">
          </th>
      </tr>
      <tr> 
        <th>Last Name</th>
        <th> 
            <input type="text" name="last"  value="<?php echo $last ?>" size="20">
          </th>
      </tr>

      <tr> 
        <th>
        <th>  
            <input type="submit" name="next" value="Continue>>">
          </th>
      </tr>
    </table>
  </div>
  
</form>

<?php } 

if(!isset($first)) {
        show_form();
}
else {
   if(empty($first) or empty($last)) {
        echo "You did not fill in all the 
                fields, try again<p>"; 
        show_form($first,$last);
   }
   else {
                !!!NEED TO LAUNCH NEXTPAGE.PHP HERE IF ALL FIELDS ARE FILLED IN!!!
	//header('Location: NEXTPAGE.php');
	//echo "NEXTPAGE.php?last=$last&first=$first"

   }
} 
?>


</body>
</html>
Your help is greatly appreciated!!!

Posted: Mon Oct 03, 2005 12:28 am
by pilau
3 things:

1) Welcome.
2) Why not in the PHP - Code forum?
3) Why not use PHP tags when posting :roll:

Oh and a tip: use <?php ?>, not <? ?> tags in your code.

Posted: Mon Oct 03, 2005 12:49 am
by kristolklp
1) Thanks.
2) Can a Mod move this post to the correct forum? Sorry.
3) Edited the CODE tags to PHP

Oh, thanks for the tip...I'll keep that in mind!

Posted: Mon Oct 03, 2005 8:02 am
by d3ad1ysp0rk
I rewrote your code a bit, included a better error handling method, and set it up to transfer the variables via GET (since I assume you don't know how to use sessions yet).

Code: Select all

<?php
$first = "";
$last = "";
$error = "";
if(!empty($_POST)) {
	if(!empty($_POST['first']) && !empty($_POST['last'])){
		header("Location:nextPage.php?first=".$_POST['first']."&last=".$_POST['last']);
	}
	else {
		$error = "Error: You must fill in both your first and last name.<br/>";
		$first = $_POST['first'];
		$last = $_POST['last'];
	}
}
?>
<html>
<head>
<title>Place Order</title>
</head>
<body>
<?php echo $error;?>
<form method="post" action="thisPage.php" enctype="multipart/form-data">
  <div align="left"><strong>PLACE ORDER</strong></div>
      <table>
      <tr>
        <th>First Name</th>
        <th>
            <input type="text" name="first" value="<?php echo $first;?>"  size="20">
          </th>
      </tr>
      <tr>
        <th>Last Name</th>
        <th>
            <input type="text" name="last"  value="<?php echo $last;?>" size="20">
          </th>
      </tr>

      <tr>
        <th>
        <th>  
            <input type="submit" name="next" value="Continue>>">
          </th>
      </tr>
    </table>
  </div>
</form>
</body>
</html>

Posted: Mon Oct 03, 2005 9:06 am
by kristolklp
Thank you for your help, I appreciate it. I assume you mean, I need to use '$_GET' on my second page to retrieve the variables from the first since you are using '$_POST' in teh code? I

I would like to add a 'back' button and save the information through the pages so maybe I need to look further into session variables.

Thanks again fro your help!