views:

148

answers:

4

i have a php page, on that page i have text boxes and a submit button, this button runs php in a section:

if(isset($_POST['Add'])){code}

This works fine here and in that section $name,$base,$location etc are calculated and used. but that section of code generates another submit button that drives another section of code.

it is in this second section of code that i wish to add data to the DB. Now i already know how to do this, the problem is that the variables $name and so forth have a value of NULL at this point.. but they can only be called after the first code section has been run where they gain value.

How do i maintain these values until the point where i add them?

Resources:
the page feel free to try it out: location mustbe of the form 'DNN:NN:NN:NN' where D is "D" and N is a 0-9 integer
http://www.teamdelta.byethost12.com/postroute.php

the code of the php file as a text file!
http://www.teamdelta.byethost12.com/postroute.php
lines 116 and 149 are the start of the 2 button run sections!

+2  A: 

you could store them in a session

// first part of form, store name in session
$_SESSION['name'] = $_POST['name'];

// 2nd part of form, store in database
$name = mysql_real_escape_string($_SESSION['name']); 
$sql = "INSERT INTO table (name_column) VALUES ('$name');
bumperbox
the session also seem to be empty very odd!
Arthur
You have to call session_start() on every "page" that needs access to the session data. You should also set error_reporting=E_ALL and display_errors=On on your development server to see any "warning: cannot send header yadda yadda" messages (or keep an eye on the error.log)
VolkerK
+1  A: 

I think you are looking for PHP's session handling stuff ...

As an example, first page:

session_start(); # start session handling.
$_SESSION['test']='hello world';
exit();

second page:

session_start(); # start session handling again.
echo $_SESSION['test']; # prints out 'hello world'

Behind the scenes, php has set a cookie in the users browser when you first call session start, serialized the $_SESSION array to disk at the end of execution, and then when it receives the cookie back on the next page request, it matches the serialised data and loads it back as the $_SESSION array when you call session_start();

Full details on the session handling stuff:

http://uk.php.net/manual/en/book.session.php

benlumley
+2  A: 

Around the add-button you are creating a second form. If you want to have the data within this form then you will have to create hidden input fields. Because you are sending a second form here. Or you are moving the add button up to the other form.

Or as others are mentioning.. Save the values into a session.

Raffael Luthiger
+1  A: 

You can also try using hidden forms variables to store the data

Extrakun