[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

Using php to refresh page w/ new data

I know you can use ajax to do this but is there a way to submit a form and return to the same page with updated information.

I have a review page, I want the user to select the job then hit submit. Then query the database and return the results and allow them to edit and then submit any changes.

If so could you please provide a small example or explanation of steps. Thanks
0
teedo757
Asked:
teedo757
1 Solution
 
haloexpertsexchangeCommented:
you don't have to use ajax for that. You can just do a post back to the same page.
do a
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
have the user fill in answers in the form and submit it.
Then on the next time around check for the posted data so an if(!empty($_POST['field']))
you can then show the information from your database query and then build a new form with that information which they can then edit and submit again for whatever final processing you need.
0
 
Mohamed AbowardaSoftware EngineerCommented:
You can use AJAX with POST or GET method to submit data to the server and then get the response after the server process the request.

Take a look here:
http://www.openjs.com/articles/ajax_xmlhttp_using_post.php
0
 
Jagadishwor DulalBraces MediaCommented:
Why not use meta refresh it's easy to do.
<?
//write your code to submit your form first and if your form submit with valid data then  write code like below:
echo "<meta http-equiv='refresh' content='0;URL=url-of-your-page'>";
?> 

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Lukasz ChmielewskiCommented:
Try this:

<?php
   // based on entered text but you can use the query to assign a value to $sometext variable
   if(!empty($_POST)) $somevalue = $_POST[entertext]; 
?>

<form method="post" action="<?php $_SERVER['PHP_SELF']?>">
<input type="text" name="entertext" />
<input type="text" name="newvalue" value="<?php echo"$sometext"; ?>" />
<input type="submit" />
</form>

Open in new window

0
 
Ray PaseurCommented:
The general design would go something like this:

Initialize in-memory values
Query the data base and overwrite the in-memory values, as found in the data base
Merge the POST array, if any, with the in-memory values
Write the data base using the in-memory values
Present the form using the in-memory values

In this pattern we update the data base after every submit, so whatever the client put into the form gets put into the data base and it is all up-to-date.  Here is an example that uses the session array instead of the data base.  The principles are the same.
http://www.laprbass.com/RAY_form_session.php
<?php // RAY_form_session.php


// SHOW HOW TO COPY VARIABLES FROM AN INPUT FORM INTO THE SESSION ARRAY,
// SO THE FORM DATA IS AVAILABLE ON EVERY PAGE OF A WEB SITE.  ANOTHER
// IMPLEMENTATION OF THIS CONCEPT WOULD COPY THE VARIABLES INTO A DATA BASE
// TABLE


// UNCONDITIONALLY START THE SESSION ON EVERY PAGE
session_start();

// FOR READABILITY
echo "<pre>";

// SET ERROR REPORTING TO SUPPRESS THE NOTICES THAT COME FROM USING UNDEFINED VARS
error_reporting(E_ALL ^ E_NOTICE);

// SHOW THE VALUES IN THE SUPERGLOBAL ARRAYS
echo "\nINITIAL VALUES IN _SESSION: ";
var_dump($_SESSION);

echo "\nINITIAL VALUES IN _POST: ";
var_dump($_POST);


// THIS IS THE ACTION SCRIPT.
// IF THERE IS SOMETHING IN $_POST
if (!empty($_POST))
{
    // ASSUME THERE ARE NO ERRORS
    $error = ''; // EMPTY STRING

    // COPY THE FORM FIELDS INTO THE SESSION
    $_SESSION["f1"] = $_POST["f1"];
    $_SESSION["f2"] = $_POST["f2"];

    // TEST FOR ACCEPTABLE INPUT VALUES - ACCEPT ONLY GOOD VALUES
    if (strtoupper($_POST["f1"]) != 'ABC') { $error .= '<br/>FIELD 1 DID NOT CONTAIN ABC'; }
    if (strtoupper($_POST["f2"]) != 'DEF') { $error .= '<br/>FIELD 2 DID NOT CONTAIN DEF'; }

    // ANY ERRORS?
    if ($error != '')
    {
        echo $error;
        echo "\nPlease <a href=\"{$_SERVER["PHP_SELF"]}\">try again</a>";
    }

    // NO ERRORS
    else
    {
        echo "\nALL FIELDS ARE VALID.  HERE ARE THE VALUES IN _SESSION NOW: ";
        var_dump($_SESSION);
    }
}
// END OF PHP PROCESSING
?>

THIS IS THE FORM - NOTE HOW SESSION VALUES ARE USED TO FILL IN THE FIELDS
<form method="post">
FIELD 1 => PUT IN 'ABC' <input name="f1" value="<?php echo $_SESSION["f1"]; ?>" />
FIELD 2 => PUT IN 'DEF' <input name="f2" value="<?php echo $_SESSION["f2"]; ?>" />
<input type="submit" value="Go!" />
</form>

Open in new window

0
 
teedo757Author Commented:
<?php
   // based on entered text but you can use the query to assign a value to $sometext variable
   if(!empty($_POST)) $somevalue = $_POST[entertext]; 
?>

<form method="post" action="<?php $_SERVER['PHP_SELF']?>">
<input type="text" name="entertext" />
<input type="text" name="newvalue" value="<?php echo"$sometext"; ?>" />
<input type="submit" />
</form>

Open in new window


I could not get this to work. Putting text into the first box and submitting did nothing.
0
 
Ray PaseurCommented:
Yes, I think I can understand.  The variable names do not line up very well.  Try this snippet instead (I have not tested it).
<?php
error_reporting(E_ALL);
$somevalue = NULL;
if(!empty($_POST["somevalue"])) $somevalue = $_POST["somevalue"]; 
?>

<form method="post">
<input type="text" name="somevalue" value="<?php echo $somevalue; ?>" />
<input type="submit" />
</form>

Open in new window

0
 
teedo757Author Commented:
Example worked great. Thanks
0
 
Ray PaseurCommented:
Thanks for the points and best of luck with your project, ~Ray
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now