Jump to content

passing an array from one page to another page


jbond

Recommended Posts

Hi all,

I am trying to pass an array of elements (gathered from a form) from one page to another page where they will be inserted into an mysql db.

I haven't got the faintest clue on how to do this. When submitting the form, the second page is called but no data is passed. I read a lot of stuff about serialization etc, but how this works, I simply don't understand. Has anyone had such a situation where data from an array needs to go from one page to another and how can this be done?

It is not clear if you are talking about your form sending data to the form processing page or your form processing page sending data to another page.

 

If the problem is between your form and your form processing page, you would need to post your code for your form and your form processing page.

 

If your question is about passing data from your form processing page to another page, either use a session or put all the code related to the form data on your form processing page (that is why the target page of a form is called the form processing page.)

I would use sessions.

 

In the first script:

<?php
session_start();
$_SESSION['post'] = $_POST;
?>

in the second script:

<?php
session_start();
?>

and then reference $_SESSION['post']['form_field_name'] instead of $_POST['form_field_name']

 

Ken

I think for this particular case, you are better off using $_GET or $_POST. Using $_SESSION has the drawn back of unexpected behavior if there is a page refresh and data lingering while it shouldn't. You should be able to do something like the following:

$myarray = array(...something data...);
$myarrayasstring = htmlentities(serialize($myarray));

<a href="somelink.php?arraydata=<?= $myarrayasstring; ?>">Send</a>

...

$data = unserialize(htmlentity_decode($_GET['arraydata']));

 

I might have misspelled some of the function names but you get the idea.

thank you all for your input.

I had following in mind

(a) routine which holds the form where the user can input his data. Multicon being the array that holds the data.

    on its turn, it calls another program add_multicon.php

// multicon.html

<body>

<form method="post" name="form1" action="add_multicon.php">

<input type="text" name="multicon[]" />

<input type="text" name="multicon[]" />

<input type="text" name="multicon[]" />

<input type="text" name="multicon[]" />

<input type="text" name="multicon[]" />

<input type="text" name="multicon[]" />

<input type="text" name="multicon[]" />

<input type="text" name="multicon[]" />

<input type="text" name="multicon[]" />

<input type="submit" />

</form>

</body>

 

(b) add_multicon.php

<?php

// analyse what came in from the form

// query still needs to be activated

for($i=0;$i<count($multicon);$i++){

echo $multicon[$i];

if($multicon[$i] == "") break;

// mysql_query("INSERT INTO table...");

}

?>

 

 

problem that I now have is that the called routine (add_multicon.php) doesn't reckognize the array. probably I am missing something somewhere, but can't find what is causing the problem...

Figured it out, using the $_POST command as suggested by a couple of forum members, for which thank you very much

Below is the code that works, should anyone, facing the same problem, be interested in

 

<?php

// analyse what came in from the form

// query still needs to be activated

if($_POST['submit']) {

$vars=count($_POST['multicon']);

for($i=0;$i<$vars;$i++){

echo ($_POST['multicon'][$i]);

echo "\r";

if($_POST['multicon'][$i] == "") break;

// mysql_query("INSERT INTO table...");

}

}

?>

 

<form method="post">

<input type="text" name="multicon[]" />

<input type="text" name="multicon[]" />

<input type="text" name="multicon[]" />

<input type="text" name="multicon[]" />

<input type="text" name="multicon[]" />

<input type="text" name="multicon[]" />

<input type="text" name="multicon[]" />

<input type="text" name="multicon[]" />

<input type="text" name="multicon[]" />

<input type="text" name="multicon[]" />

<input type="text" name="multicon[]" />

<input type="submit" value="submit" name="submit"/>

</form>

 

 

Thank you to everyone for their input..

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.