Jump to content

Recommended Posts

how to do I remain in same page after calling a PHP program

 

example:

 

file PS4.PHP calls addRecord.php like in below as an action.  right now I go to addRecord but would like to remain in PS4.PHP

 

<form method="post" action="addRecord.php" >

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

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

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

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;
}

yeah but its kinda hard to store an array in get ;)

 

i usually store my errors in an array incase i have more than 1

 

good call....truly, i usually have the page post to itself, so there is no need to redirect if there is an error :)

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;
}

?>

Is there a way of a page redirecting to a specific part of iteself without having to reload?

 

what do you mean? you can use anchor tags to move around in a page. or you can use ajax to submit/retrieve data without a page reload.

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.