Link to home
Start Free TrialLog in
Avatar of glassd
glassd

asked on

Processing data before calling next script

I'm writing a short application which saves data to a database and then retrieves it.

What I want to do is list the data based on certain criteria. So I have a form which asks certain questions. What I want to do is to take the input data from these questions and process it before calling the script which generates the list. The only way I can see of doing this is to have an intermediate form which does the processing and then sets some hidden variables before calling the listing script. However this form must then have its own submit button, an unecessary step for the user.

Is there any way of automatically submitting a form, without user interaction, or is there a better way of doing this.

Thanks
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Isn't it more logical to separate the writing from the reading essentialy? Otherwise people - just because they do - hit the F5/refresh to update your listview, adding data to your db, because they don't remember they did input some data to store earlier.

-r-
Avatar of Diablo84
Diablo84

This is one method, not necessarily the best because it requires using the query string but it should work for you:

point your form to process.php (ie the action) for example, process.php will look like this:

$data = $_POST['input_name']; //do this for each
$data2 = $_POST['input_name2'];

$query = mysql_query ("INSERT INTO tablename (filedname,fieldname,etc) VALUES ('$data','$data2','etc')") or die (mysql_error());

header ("location: page.php?var1=$data&var2=$data2");
exit;

This page will process the sent form data, run the query (all of which you will have to modify to work for your system) then redirects the header back to your page which you want to run the select query on (in the example - page.php).

On the select query page you can get the values using

$data1 = $_GET['var1'];
$data2 = $_GET['var2'];

and so on for as many is needed, you can then plug these variables into the query, for example:

$query = "SELECT * FROM tablename WHERE somefield = '$data1' AND anotherfield = '$data2'";
Avatar of glassd

ASKER

Sorry, slight misunderstanding. This process formats data input by the user and then generates a list based on that data. There is no write to the database.

PS. I manage the F5 problem using cookies.
Avatar of glassd

ASKER

Okay, more information.

My listing script looks something like this:

$Result = mysql_query("select column from table $Search;");

I take the user selection from the input form and process it with the intermediate form, coming up with a string which looks something like this:
' where column = "value" '
which is then passed on to the listing script.

which is then passed to the listing. This means I can use different input forms for different purposes and they will generate the search ctring. Or I can call the listing script directly and get everything.
ASKER CERTIFIED SOLUTION
Avatar of Diablo84
Diablo84

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Posted above before your follow up comment, il come back to this in a minute if necessary, have to point my attention to something else for a moment.
Ok, another method which may be more useful to you is using sessions. The session data will be globally available around your site all the time the session is active so it will mean you will not need to submit form data any more times then is necessary.

session_start(); //top of each page that uses the session data

$_SESSION['var_name'] = "value";

When form data is posted for the first time you can assign it to a session variable so that it is available throughout your scripts from that point fourth, you can also assign any data that is outputted as a result of processing to a sesson variable

$_SESSION['variable'] = $_POST['variable'];

You can then use that data anywhere using $_SESSION['variable'] as long as session_start(); is at the top of the page.

If you have processing occuring on seperate pages you can also use the header redirect - header("location: page.php"); - where needed.
Avatar of glassd

ASKER

Thanks for the advice.

I used the
header ("location: page.php?var1=$data&var2=$data2");
method and that works fine for what I am doing.

I also had huge problems dealing with strings containing single and double quotes, especially passing them between scripts with the
<input type=hidden ... >
method. The session variable method worked well.

Cheers
no problem :)