mikeleblanc Posted December 3, 2008 Share Posted December 3, 2008 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" > Quote Link to comment https://forums.phpfreaks.com/topic/135415-how-to-remain-in-same-page-after-callig-a-php-program/ Share on other sites More sharing options...
adx Posted December 3, 2008 Share Posted December 3, 2008 You could do it like this: <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> This will load whatever input to the same page as you're already on. Quote Link to comment https://forums.phpfreaks.com/topic/135415-how-to-remain-in-same-page-after-callig-a-php-program/#findComment-705348 Share on other sites More sharing options...
rhodesa Posted December 3, 2008 Share Posted December 3, 2008 you need an echo in there: <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> or just leave the action empty...it will default to itself: <form method="post" action=""> Quote Link to comment https://forums.phpfreaks.com/topic/135415-how-to-remain-in-same-page-after-callig-a-php-program/#findComment-705350 Share on other sites More sharing options...
gathos Posted December 3, 2008 Share Posted December 3, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/135415-how-to-remain-in-same-page-after-callig-a-php-program/#findComment-705374 Share on other sites More sharing options...
mikeleblanc Posted December 3, 2008 Author Share Posted December 3, 2008 But where do I call my addRecord.php file? Quote Link to comment https://forums.phpfreaks.com/topic/135415-how-to-remain-in-same-page-after-callig-a-php-program/#findComment-705381 Share on other sites More sharing options...
chronister Posted December 3, 2008 Share Posted December 3, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/135415-how-to-remain-in-same-page-after-callig-a-php-program/#findComment-705403 Share on other sites More sharing options...
premiso Posted December 3, 2008 Share Posted December 3, 2008 You may be wanting to do an AJAX request, where you can send data to a page and not do a page reload. My suggestion is look into AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/135415-how-to-remain-in-same-page-after-callig-a-php-program/#findComment-705407 Share on other sites More sharing options...
blueman378 Posted December 3, 2008 Share Posted December 3, 2008 could you not just use a header to redirect back to where you were? users would never know they left the page Quote Link to comment https://forums.phpfreaks.com/topic/135415-how-to-remain-in-same-page-after-callig-a-php-program/#findComment-705422 Share on other sites More sharing options...
rhodesa Posted December 3, 2008 Share Posted December 3, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/135415-how-to-remain-in-same-page-after-callig-a-php-program/#findComment-705480 Share on other sites More sharing options...
blueman378 Posted December 4, 2008 Share Posted December 4, 2008 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, Quote Link to comment https://forums.phpfreaks.com/topic/135415-how-to-remain-in-same-page-after-callig-a-php-program/#findComment-705774 Share on other sites More sharing options...
rhodesa Posted December 4, 2008 Share Posted December 4, 2008 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; } Quote Link to comment https://forums.phpfreaks.com/topic/135415-how-to-remain-in-same-page-after-callig-a-php-program/#findComment-705935 Share on other sites More sharing options...
blueman378 Posted December 5, 2008 Share Posted December 5, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/135415-how-to-remain-in-same-page-after-callig-a-php-program/#findComment-706387 Share on other sites More sharing options...
rhodesa Posted December 5, 2008 Share Posted December 5, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/135415-how-to-remain-in-same-page-after-callig-a-php-program/#findComment-706691 Share on other sites More sharing options...
blueman378 Posted December 6, 2008 Share Posted December 6, 2008 yeah thats my normal thing except like login scripts which have multiple forms Quote Link to comment https://forums.phpfreaks.com/topic/135415-how-to-remain-in-same-page-after-callig-a-php-program/#findComment-707490 Share on other sites More sharing options...
webmaster1 Posted December 6, 2008 Share Posted December 6, 2008 Is there a way of a page redirecting to a specific part of iteself without having to reload? Quote Link to comment https://forums.phpfreaks.com/topic/135415-how-to-remain-in-same-page-after-callig-a-php-program/#findComment-707491 Share on other sites More sharing options...
redarrow Posted December 6, 2008 Share Posted December 6, 2008 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/135415-how-to-remain-in-same-page-after-callig-a-php-program/#findComment-707511 Share on other sites More sharing options...
webmaster1 Posted December 6, 2008 Share Posted December 6, 2008 'NEXT QUISTION' As if spoken in a Hollandic accent. Quote Link to comment https://forums.phpfreaks.com/topic/135415-how-to-remain-in-same-page-after-callig-a-php-program/#findComment-707537 Share on other sites More sharing options...
rhodesa Posted December 7, 2008 Share Posted December 7, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/135415-how-to-remain-in-same-page-after-callig-a-php-program/#findComment-708565 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.