How To Run PHP Script With Form Button and Stay on Same Page

lleblanc65
lleblanc65 used Ask the Experts™
on
I have a button that runs a PHP script that updates prices on my page. Right now the button redirects to the update page (priceupdate.php). I want the button to run this script, but not redirect to it. It'd even greater if the button's name could change from "Update Prices" to "Prices Updated" after it's been clicked.

I'm aware I'll need to use some javascript, but after some heavy googling I haven't found quite what I'm looking for. Here is what I have so far.

<form action="updateprices.php" method="post">
  <input value="Update Prices" type="submit">
</form>

Open in new window


Any suggestions? I don't have any text to submit, which is the majority of what I've been finding sample scripts for.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
IT Manager
Commented:
Sure thing...you will need to have all of the code from the priceupdate.php on the same page as the one you want to stay on...as far as updating the information..


The only thing you are going to do different is change the form action to:
 
<form name="test" method="post" action="<?php $_SESSION['PHP_SELF']?>">

Open in new window


This will refresh the page...

Now you need to also add a hidden input value in form to submit to make sure you know when you have come to the page for the first time or if it's after you submit the page.

I'm going to write a custom page for you to show you and then submit it.
Carlos LlanosIT Manager

Commented:
Here's the code for a simple price update page.

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>


<?php

$check = '';
$price = '$4.99';

if (isset($_POST['check'])) {
	$check = $_POST['check'];	
}

if (isset($_POST['price'])) {
	$price = '$' . $_POST['price'];
}

if ($check != '1') {

?>
<h1>Price is currently set to: <?php print $price;?></h1>	
<form action='<?php $_SESSION['PHP_SELF'];?>' method="POST">
<table>
<tr>
	<td>Price: <input type="text" name='price' id='price' /></td>
</tr>
<tr>
	<td><input type="hidden" name='check' id='check' value='1' /></td>
</tr>
<tr>
	<td><input type="submit" name='submit' id='submit' value='Update Price' /></td>
</tr>
</table>

</form>
<?php 
} else if ($check == '1') {

?>

<h1>Price has been updated to: <?php print $price;?></h1>	
<form action='<?php $_SESSION['PHP_SELF'];?>' method="POST">
<table>
<tr>
	<td>Price: <input type="text" name='price' id='price' /></td>
</tr>
<tr>
	<td><input type="hidden" name='check' id='check' value='1' /></td>
</tr>
<tr>
	<td><input type="submit" name='submit' id='submit' value='Update Price' /></td>
</tr>
</table>

</form>
<?php	
}
?>
</body>
</html>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial