Jump to content

how to remain in same page after callig a PHP program


mikeleblanc

Recommended Posts

I mocked up this code, hopefully it will help, even though it's not a direct answer.

<html>
<head>
<title>Untitled Document</title>
</head>

<body>
<form method='post'>
<?php
$width = $_POST["width"];
$height = $_POST["height"];
?>

<br />
<br />
Width:
<input type='text' name='width' />
<br />
X
<br />
Height :
<input type='text' name='height' />
<br />
<input type='submit' name='submit' value='Calculate' />
<br />
<br />
answer: 
<?	
if(!empty($_POST)){ 
$total = $width * $height;
echo $total;
};
// to make sure not to display anything until you input a number
?>
</form>
</body>
</html>

 

later days,

gathos

Link to comment
Share on other sites

Is add record the file that processes this form? If it is, and if you have conditionals in there to ensure that the form is submitted before you start processing it, then you can simply include it at the top of the page that the form is on.

 

   <?php include('addRecord.php'); ?>

 

 

I try to keep forms and the processing for it on the same page. Does not always happen, but if it is a simple form then I keep it all together and use if statements to determine what to do when.

 

Nate

Link to comment
Share on other sites

could you not just use a header to redirect back to where you were?

 

users would never know they left the page

 

this sounds like what you want.

 

-PS4.PHP has a form

-Form submits to addRecord.php

-addRecord.php processes the data

-addRecord.php redirects back to PS4.PHP

 

at the end of addRecord.php, put this:

header('Location: PS4.PHP');
exit;

for it to work, there can't be anything printed on addRecord.php

Link to comment
Share on other sites

to extend what rhodesa has said, if you want to display say an error message you would have addRecord.php set a session with the error message, then have PS4.php display it,

 

i usually pass the error via GET:

if(empty($email)){
  header('Location: PS4.PHP?error=no_email');
  exit;
}

Link to comment
Share on other sites

Try this one page game, I made for fun......

 

Your see the $_GET in action to call the multipule forms.....

 

<?php session_start();

echo "<a href='".$_SERVER['PHP_SELF']."?game=game'>Play the data game</a>";

if(isset($_POST['submit1'])){

$name=$_POST['name'];

if($_POST['name']){

$_SESSION['name']=$name;
}
}

if(isset($_POST['submit2'])){

$age=$_POST['age'];

if($_POST['age']){

$_SESSION['age']=$age;
}
}

if(isset($_POST['submit3'])){

	$country=$_POST['country'];

if($_POST['country']){

$_SESSION['country']=$country;
}
}

if($_GET['game']=="game"){

echo "<center>

<form method='POST' action='".$_SERVER['PHP_SELF']."?form=form1'>

<br><br>

your name please

<br><br>

<input type='text' name='name'>

<br><br>

<input type='submit' name='submit1' value='NEXT QUISTION'></center>";


exit;
}


if($_GET['form']=="form1"){

echo "<center>

<form method='POST' action='".$_SERVER['PHP_SELF']."?form=form2'>

<br><br>

your age please

<br><br>

<input type='text' name='age'>

<br><br>

<input type='submit' name='submit2' value='NEXT QUISTION'></center>";

exit;
}

if($_GET['form']=="form2"){

echo"<center>

<form method='POST' action='".$_SERVER['PHP_SELF']."?form=form3'>

<br><br>

your country please

<br><br>

<input type='text' name='country'>

<br><br>

<input type='submit' name='submit3' value='GET RESULTS'></center>";

exit;
}


if($_GET['form']=="form3"){


echo "<center><br><br><a href='".$_SERVER['PHP_SELF']."?result=results'>SHOW RESULTS</a><br><br></center>";

}

if($_GET['result']=="results"){

echo " <br><br>

<center>Your name is ".$_SESSION['name']." and your age is ".$_SESSION['age']." and your country is ".$_SESSION['country']." </center>

<br><br>";

exit;
}

?>

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.