Solved

Processing data before calling next script

Posted on 2004-09-23
9
215 Views
Last Modified: 2008-03-06
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
0
Comment
Question by:glassd
  • 5
  • 3
9 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 12131519
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-
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12131555
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'";
0
 
LVL 7

Author Comment

by:glassd
ID: 12131578
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.
0
 
LVL 7

Author Comment

by:glassd
ID: 12131614
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.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 27

Accepted Solution

by:
Diablo84 earned 250 total points
ID: 12131620
Sorry your first line in the question threw me, nonetheless you should still be able to employ the same method:

$data = $_POST['input_name']; //do this for each
$data2 = $_POST['input_name2'];
//process the data here
header ("location: page.php?var1=$data&var2=$data2");
exit;

Then for the header line you can add the output of your processing script to the query string and access the values on page.php using $_GET['varname'] as shown above.

Failing that, back to basics

>> Is there any way of automatically submitting a form, without user interaction

You can use <body onLoad="document.forms.form_name.submit();">
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12131626
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.
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12131998
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.
0
 
LVL 7

Author Comment

by:glassd
ID: 12168022
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
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12168044
no problem :)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now