Jump to content

[SOLVED] I hate arrays!


brooksh

Recommended Posts

Can someone please help me. I cannot seem to figure this out. I have a form with a multiple dropdown. I am trying to post it to another page, but have a back button to redo the form if needed. Here is what I have, but it does not work.

 

<form name="form" method="post" action="page2.php">
<?
if($numbers!="")
{
$numbers2 = explode(",", $numbers);
}
else
{
$numbers2[] ="";
}
?>
<select name="numbers[]" multiple size="4">     
<option value="one" <?php if(in_array("one", $numbers2)){echo "selected";} ;?>>one</option>
<option value="two" <?php if(in_array("two", $numbers2)){echo "selected";} ;?>>two</option>
<option value="three" <?php if(in_array("three", $numbers2)){echo "selected";} ;?>>three</option>
<input type="submit" value="Submit">

 

 

On Page 2 I have:

<form name="back" method="post" action="page1.php">
<input type="hidden" name="numbers[]" value="<?=$numbers?>">
<input type="submit" value="submit">
</form>

Link to comment
https://forums.phpfreaks.com/topic/140867-solved-i-hate-arrays/
Share on other sites

@gevans that is what it is looking like.

 

@brooksh I'm not seeing that you are posting ( $_POST ) the array. Which is causing it to not get a value. Make sure you post it the rest looks correct from a quick glance.

 

Why not have the form fail and not go to the next page if not successful?

Link to comment
https://forums.phpfreaks.com/topic/140867-solved-i-hate-arrays/#findComment-737324
Share on other sites

Just a note, because there are numerous problems with this.... I'd suggest you look into sessions..

 

//page two

session_start();

$_SESSION['your_name'] = 'get_or_post_data';

 

//page one

session_start();

echo '<input type="text" name="name" value="'.$_SESSION['the_right_session'].'" />';

Link to comment
https://forums.phpfreaks.com/topic/140867-solved-i-hate-arrays/#findComment-737327
Share on other sites

first off, you can't just send an array as information through POST. all POST values are strings, and therefore if you want to reconstruct the array on page2.php, you'll have to make each item manually, like so:

 

<?php
foreach ($_POST['numbers'] AS $value)
{
  echo '<input type="hidden" name="numbers[]" value="'.$value.'" />';
}
?>

 

between your form tags.

 

secondly, you are neglecting the fact that variables sent through a form will be in the $_POST[] array, not local variables, unless you have register_globals on which is rather unlikely since it is no longer the default setting. when you refer to the $numbers array, you'll need to use its full designation:

 

<?
if(empty($_POST['numbers']))
{
   $_POST['numbers'] = array();
}
?>
[...]
<option value="one" <?php if(in_array("one", $_POST['numbers'])){echo "selected";} ;?>>one</option>

 

FINALLY, start using full tags instead of short tags (that is, use <?php rather than <?), because short tags are deprecated and will soon lose support entirely.

 

i'm not sure where you are learning from, but it would do you a world of good to start focusing on proper notation and conventions, as this will solve half of your problems right off the bat.

Link to comment
https://forums.phpfreaks.com/topic/140867-solved-i-hate-arrays/#findComment-737328
Share on other sites

Thanks for the Help.

 

Using

<?php
foreach ($_POST['numbers'] AS $value)
{
  echo '<input type="hidden" name="numbers[]" value="'.$value.'" />';
}
?>

 

And Using This it works.

 <option value="one" <?php if(in_array("one", $_POST[numbers])){echo "selected";} ;?>>one</option>

Link to comment
https://forums.phpfreaks.com/topic/140867-solved-i-hate-arrays/#findComment-737333
Share on other sites

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.