[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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

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
0
glassd
Asked:
glassd
  • 5
  • 3
1 Solution
 
RoonaanCommented:
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
 
Diablo84Commented:
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
 
glassdAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
glassdAuthor Commented:
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
 
Diablo84Commented:
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
 
Diablo84Commented:
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
 
Diablo84Commented:
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
 
glassdAuthor Commented:
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
 
Diablo84Commented:
no problem :)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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