Page 1 of 1

POST Array contents

Posted: Fri Aug 27, 2010 10:37 am
by IGGt
How do I POST a value across to another page?

I have an array, part of which is updated during the script, and I am looking to pass that value across when the page updates 60 seconds later.

So far I have:

Code: Select all

$connections_array[] = array('server' => '123.456.789.012:3306',
                             'user' => $u,
                             'password' => $p,
                             'database' => 'MySQL01',
                             'sendmail' => '0',
                             'dbID' => '01'
                                           );
//and repeat multiple times


for($i = 0; $i <sizeof($connections_array); $i++) {
//Execute MySQL Query and if result x is achieved
$connections_array[$i]['sendmail'] = '1';

I presume at this point I need to put my array data into a form, so I have:

Code: Select all

Print "	<form name=\"SendMailState\" action=\"testPage.php\" method=\"post\">\n ";
	for($a = 0; $a <sizeof($connections_array); $a++) {
		$SendState = $connections_array[$a]['sendmail'];
		$SendDB = $connections_array[$a]['dbID'];    	
			print "	<INPUT TYPE=\"hidden\" NAME=\"$SendDB\" VALUE=\"$SendState\"><br/>\n ";
    	}
print "	</form>\n";
But here I get stuck. I figure I need to change the Array so that it looks something like:

Code: Select all

$connections_array[] = array('server' => 'localhost:3306',
                             'user' => $u,
                             'password' => $p,
                             'database' => 'MySQL01_5083',
                             'sendmail' => '0',
                             'dbID' => '01');

if (isset($_POST['01'])) {
     $connections_array[]['sendmail'] = '1';     

But this doesn't work. I can see a few problems, but can't see how to resolve them?

Re: POST Array contents

Posted: Fri Aug 27, 2010 11:48 am
by Jonah Bron
It might be easier to store the information in the $_SESSION variable.

Re: POST Array contents (now using $SESSION)

Posted: Tue Aug 31, 2010 5:57 am
by IGGt
Cheers, I've had a look at that , and it seems to make sense, but as yet, it's not seeming to work. I think I must be missing something. What I have done is:

Code: Select all

session_start(); 

//set $connections_array, repeat x 10
$connections_array[0] = array('server' => 'localhost:3306',
                             'user' => $u,
                             'password' => $p,
                             'database' => 'MySQL01_5083',
                             'sendmail' => '0',
                             'dbID' => '01'
                                           );

//check if $session array[$a] is set and update $connections_array['sendmail'] accordingly
for($a = 0; $a <sizeof($connections_array); $a++) {
	if (isset($_SESSION[$a])) {
		$sm = (isset($_SESSION[$a]));
		$connections_array[$a]['sendmail'] = $sm;     }
	else 	{
		$connections_array[$a]['sendmail'] = 0;
			}
	} 

//run throgh the rest of the process, and set $connections_array['sendmail'] based on the outcome
//create connection to DB, run query
if ($row['Slave_IO_Running'] == "No")	{
				print "	<tr $colour1>\n";
					if ($connections_array[$i]['sendmail'] == '0') 	{
											$connections_array[$i]['sendmail'] = '1';
																	} 
					elseif ($connections_array[$i]['sendmail'] == '1') 	{
											$connections_array[$i]['sendmail'] = '2';
																		} etc. etc.

//finally, set the $session array with the current value of $connections_array['sendmail']
for($a = 0; $a <sizeof($connections_array); $a++) {
	$sm2 = $connections_array[$a]['sendmail'];
	$SESSION[$a] = $sm2;
	}
but I have placed a hidden form at the start and end of the script to show the value of $connections_array['sendmail'] at the beginning and end of the process, and no matter what happens, it doesn't seem to get carried over (It's always 0 at the start and 1 at the end).

Re: POST Array contents

Posted: Tue Aug 31, 2010 9:24 am
by IGGt
I think I have sussed it.

instead of using $_SESSION[$a] (which translated to $_SESSION[1], $_SESSION[2], $_SESSION[3] etc.) which it didn't like, I have changed it to this:

Code: Select all

for($a = 0; $a <sizeof($connections_array); $a++) {
	$sm2 = $connections_array[$a]['sendmail'];
	$s = 'no'.$a;
	$_SESSION[$s] = $sm2;
	}


This then translates to $_SESSION[no1], $_SESSION[no2], $_SESSION[no3] etc. which seems to work better. All I nee dto do now is apply it to my actual script to see if it still works.