Jump to content

Array passing in $Post method . Error


bobbychla

Recommended Posts

i created an array like this

 

while{

$ir = 0;

$stud[$row_mrk['id_sub']][$ir]=$row_mrk['id_sub']; $ir++;

$stud[$row_mrk['id_sub']][$ir]=$row_mrk['seminar_topic']; $ir++;

$stud[$row_mrk['id_sub']][$ir]=$row_mrk['seminar_mark']; $ir++;

$stud[$row_mrk['id_sub']][$ir]=$row_mrk['attendance']; $ir++;

$stud[$row_mrk['id_sub']][$ir]=$row_mrk['internal_mark']; $ir++;

$stud[$row_mrk['id_sub']][$ir]=$row_mrk['external_mark']; $ir++;

}

 

echo "<input type='hidden' name='ar_std' id='ar_std' value='$stud' /> </table>" ;

 

and it is able to print in the same page using

 

foreach ($stud as $v1) {

echo "$v1\n<br>";

foreach ($v1 as $v2) {

echo "$v2\n<br>";

}

}

 

and in the next page

 

$ar_stud[]=$_POST["ar_std"];

 

I'm getting an error

PHP Warning:  Invalid argument supplied for foreach() in save.php on line 25

PHP Stack trace:

Link to comment
Share on other sites

An array can't be passed directly like that, but you can serialize it:

 

<?php
$stud_serialized = serialize($stud);

echo "<input type='hidden' name='ar_std' id='ar_std' value='$stud_serialized' /> </table>" ; 

// And in your receiving script

$ar_stud = unserialize($_POST["ar_std"]);
?>

 

But you are probably better off storing it in $_SESSION

Link to comment
Share on other sites

If you pass it using a session, be sure to have a unique token attached to that request, and pass it in a hidden field.

 

$_SESSION['formData'][$token] = whatever array of data you want

 

That way, someone submitting another form in a different tab that uses sessions to hold the information won't corrupt values in the original tab. This behavior will annoy a multitasking user and be very hard to debug.

 

This is a BAD use of session though, IMO. A better way to do this would be to pass the parameters you used in your query over to the next page, and retrieve the data again. Most engines will still have the results in memory and it will be lightning fast - at LEAST as fast as retrieving the values from a flat file using sessions.

 

This is all speculative though, as I don't know how your script works in it's entirety.

Link to comment
Share on other sites

You can if your trying to do multi deletes using html. Set you checkbox input tag's name attribute to myName[] or as an example

 

<form action="del.php" method="post">
<input type="checkbox" name="ids[]" value="1" />
<input type="checkbox" name="ids[]" value="2" />
</form>

$ids = $_POST['ids'];

foreach($ids as  $id)
{

echo $id . "<br />";

}

 

There are other methods though and you may google for a function that allows you to do that or write your own.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.