Experts Round Table Network

Serverside Technology => PHP => Topic started by: thepreacher on April 26, 2007, 04:41:46 PM



Title: Want to understand the diffrent ways of returning after server script execution
Post by: thepreacher on April 26, 2007, 04:41:46 PM
I do only javascript, xhtml and php for now. One of the main struggles i have is the aftermath of submitting a form to the server side script or retrieving data from a database via a server side script.

Ill break it down here: All server side scripts are php

Situation 1. Use Ajax to fetch a row of data from database.
Current Solution: Use dot notation to join all the fields the use echo to return. Then in Ajax response function i split the returned results ans us the values.

Situation 2. same as one but this time more than one row. Maybe a whole table.
Current solution: I format the query results using JSON. Echo the results to Ajxa function in calling script the i use eval( response) to plit the results.

Situation 3. Form is submitted using javascript submit after which i perform some other task like reset the form or something in the javascript function that submitted the form.

Current solution. I dont specifically return from the server side php script (ie i don't use any return or echo in the server side script). The problem i have with this is i get a blank page instead of the from with all fields reset.

Situation 4. After submitting a form either through javascript or the submit button, I want to display another page and possibly pass some parameters to that page that will affect how the page is displayed.
Current  solution: I use the header('Location: ...') but passing parameters with is has not worked fro me so i use SESSIONS.

Situation 5. I submit a from to be validated server side, if there is a problem with any of the fields and the user needs to respond.
Current solution: Not sure what to do. Maybe join all affected fields and echo it back then split it up and redisplay the form?


that is all i can think of now but you get the general problem. I will be glad you can comment on my current solution, provide alternative(s) and educate me please? Thanks.


Title: Re: Want to understand the diffrent ways of returning after server script execut
Post by: rdivilbiss on April 26, 2007, 05:06:45 PM
1. Okay
2. You could get the results into an array the serialize the array for returning it.  It would be simpler and faster.
3,4,&5  http://www.cafesong.com/ert/form_article/index.php


Title: Re: Want to understand the diffrent ways of returning after server script execut
Post by: thepreacher on April 26, 2007, 05:44:32 PM
Wow thats alot of code :)


Title: Re: Want to understand the diffrent ways of returning after server script execut
Post by: rdivilbiss on April 26, 2007, 05:51:59 PM
Not for you.

Your part (which is mostly cut and paste is just the fields array and what to do when the processing is finished....e.g. save it, go to another page, e-mail it, or reports showing errors.

/
Code
Language: php (GeSHi-highlighted)
***************** FORM FIELD DEFINES ********************/
$fields = array();
$fields["first"] = array("value"=>"", "type"=>"name", "maxlength"=>25, "required"=>1, "label"=>"First Name", "example"=>"John", "message"=>"Please enter a name using only the letters A-Z and a hyphen or single quote if needed.");

Code
Language: php (GeSHi-highlighted)
if ($result=="") {  // if $result is empty we may perform our business logic.
/*
[color=red]1. Add new record
2. Generate e-mail verify record and send mail
3. Goto the Submission confirmation page.[/color]
*/

//$result = subscribe($email,$firstname,$lastname,$dob,$userAgent,$remoteAddress,$thisScript);
//if ($result==1) {
// $result = sendVerificationEmail($email,$userAgent,$remoteAddress,$thisScript);
//} else {
// die('ERROR 2');
//}
//header("Location: http://www.example.com/thankyou.php");
// save the current values.