Solved

Processing data before calling next script

Posted on 2004-09-23
9
224 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

821 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