Jump to content

Using an array for $_POST


viion

Recommended Posts

I need to make an array of $post values. I thought something like

 

$value[$i] = $_POST['$checkboxName[$i]'];

 

...Would work, by putting it in a for loop. My code is:

$x = 0;

for (; ; ) {

if ($x > "$possible_choice_array_totalx" -1) {

break;

}

$result[$x] = $_POST['$possible_choice_arrayx[$x]'];

echo "$x possible = $possible_choice_arrayx[$x] | $x choicex = $result[$x] <br />";

$x++;

}

 

Not working, any help on how to get a post array working.

Link to comment
Share on other sites

Why not just use foreach to iterate through the $_POST array.

 

As far as what you are trying to get it, I cannot see. But your for statement is not valid. I would look up at the manual for proper usage of that. Since it seems as though you have multiple check boxes here is an example:

 

foreach ($_POST['checkboxesname'] as $key => $val) {
    echo "Checkbox: {$key} contains a value of {$val}<br />";
}

 

 

Link to comment
Share on other sites

try this it should work

for ($x = 0; $x >  ($possible_choice_array_totalx-1) ; $x++ )
{
$result[$x] = $_POST["$possible_choice_arrayx[$x]"];
echo "$x possible = $possible_choice_arrayx[$x] | $x choicex = $result[$x] <br />";
}

 

Let me know how it goes

Link to comment
Share on other sites

Hmm, neither worked. They didn't show a report.

How do I post a PHP tag so it's coloured?

 

All the check box's are different names. depending on their value. So the Name is exactly same as the value like:

<input name='$v' type='checkbox' value='$v' /> $v

 

I tried bit snippets and adjusted them accordingly but both do not post anything. When a check box is True.

Link to comment
Share on other sites

Check box's are pulled from a function known as showChoices();

 

Which includes this:

 

// The values for possible_choice_array and users_choice_array have already been determind and so the variables are ready.
$qwert = array_values($possible_choice_array);
$qwert2 = array_values($users_choice_array);
foreach ($qwert as $v) {
if (in_array("$v", $qwert2)) {
	echo "<input name='$v' type='checkbox' value='$v' checked/> $v";
} else {
	echo "<input name='$v' type='checkbox' value='$v' /> $v";
}
}

 

An example output would be:

 

<input name="KirinsOsode" value="KirinsOsode" type="checkbox"> KirinsOsode
<input name="Kirins Pole" value="Kirins Pole" type="checkbox"> Kirins Pole

 

Then when the user hits submit it calls another function called editWishlist();

 

The code for this is so far:

 

function editWishlist() {
// Pull all SQL tables, the below values are ready to work.

while ($possiblelistx = mysql_fetch_array($possible_choice_sqlx) ) { 
	// PULL
	$possible_choicex = $possiblelistx[MONSTER_DATABASE_MOBDROP];
	// SPLIT
	$possible_choice_arrayx = explode("|", $possible_choicex);
	// COUNT
	$possible_choice_array_totalx = count($possible_choice_arrayx);


	// For statements pasted/edit here and tested.
	// EG below	
	for ($x = 0; $x >  ($possible_choice_array_totalx-1) ; $x++ )
	{
		$result[$x] = $_POST["$possible_choice_arrayx[$x]"];
		if ($result[$x]=="True") {
			echo "$possible_choice_arrayx[$x] Selcted <br />";
		}
	}
}
}

Link to comment
Share on other sites

Ok, well for one, the name has a space in it. That usually is not kosher.

 

Second you could make it an array of checkboxes by doing this:

 

foreach ($qwert as $v) {
if (in_array("$v", $qwert2)) {
	echo "<input name='checkboxname[$v]' type='checkbox' value='$v' checked/> $v";
} else {
	echo "<input name='checkboxname[$v]' type='checkbox' value='$v' /> $v";
}
}

 

Then using this:

foreach ($_POST['checkboxname'] as $key => $val) {
    echo "Checkbox: {$key} contains a value of {$val}<br />";
}

 

Should give you an idea how to use that to do your tests.

Link to comment
Share on other sites

Okay that works =D, the outcome is rather odd though. It's rendering the results 5 times. So an outcome of select 3 box's would come out like this:

 

Checkbox: KirinsOsode contains a value of KirinsOsode

Checkbox: Neptunal Abjuration: Body contains a value of Neptunal Abjuration: Body

Checkbox: Scroll of Raise III contains a value of Scroll of Raise III

Checkbox: KirinsOsode contains a value of KirinsOsode

Checkbox: Neptunal Abjuration: Body contains a value of Neptunal Abjuration: Body

Checkbox: Scroll of Raise III contains a value of Scroll of Raise III

Checkbox: KirinsOsode contains a value of KirinsOsode

Checkbox: Neptunal Abjuration: Body contains a value of Neptunal Abjuration: Body

Checkbox: Scroll of Raise III contains a value of Scroll of Raise III

Checkbox: KirinsOsode contains a value of KirinsOsode

Checkbox: Neptunal Abjuration: Body contains a value of Neptunal Abjuration: Body

Checkbox: Scroll of Raise III contains a value of Scroll of Raise III

Checkbox: KirinsOsode contains a value of KirinsOsode

Checkbox: Neptunal Abjuration: Body contains a value of Neptunal Abjuration: Body

Checkbox: Scroll of Raise III contains a value of Scroll of Raise III

Link to comment
Share on other sites

If you go here:

http://encorels.com/v2/?mode=u03character&id=963643388

Click "Kirin" then tick a few box's and then click Save Changes and you'll see the out come, you can view the source.

 

Here is the full functions that control the checkbox and the submit:

 

Showing Checkboxes:

function showChoices() {
global $possible_choice_array, $users_choice_array;

$currentID = $_GET[id];
$users_choicex = mysql_query("SELECT ". CHARACTER_CONFIG_WISHLIST .", ". CHARACTER_CONFIG_OBTAINED ." FROM ". CHARACTER_DATABASE ." WHERE ". CHARACTER_CONFIG_ID ."='$currentID'"); 
if (!$users_choicex) { echo("<div id='error'><img src='img/icons/icon_cross.png'>Users wishlist could not be loaded: " . mysql_error() . "</div>"); } 
while ($prls = mysql_fetch_array($users_choicex)) { 
	// PULL SQL
	$users_wishes = $prls[CHARACTER_CONFIG_WISHLIST];
	$users_obtained= $prls[CHARACTER_CONFIG_OBTAINED];
	// SPLIT SQL
	$users_choice_array = explode("|", $users_wishes);
	$users_obtained_array = explode("|", $users_obtained);
	// GATHER ARRAY VALUES FROM SPLIT
	$qwert = array_values($possible_choice_array);
	$qwert2 = array_values($users_choice_array);
	$qwert3 = array_values($users_obtained_array);

	foreach ($qwert as $v) {
		if (in_array("$v", $qwert3)) {
			echo "<img src='img/icons/icon_tick.png'> <strong>Obtained: $v!</strong>";
		} elseif (in_array("$v", $qwert2)) {
			echo "<input name='checkbox[$v]' type='checkbox' value='$v' checked/> $v";
		} else {
			echo "<input name='checkbox[$v]' type='checkbox' value='$v' /> $v";
		}
	}
}
}

 

On Submitting of form it calls this function:

function editWishlist() {
$currentID = $_GET[id];
// Pull Possible Choices and filter through which is true.
$possible_choice_sqlx = mysql_query("SELECT * FROM ". MONSTER_DATABASE .""); 
if (!$possible_choice_sqlx) { echo("<div id='error'><img src='img/icons/icon_cross.png'>Could not load items from data base: " . mysql_error() . "</div>"); } 

while ($possiblelistx = mysql_fetch_array($possible_choice_sqlx) ) { 
	// PULL
	$possible_choicex = $possiblelistx[MONSTER_DATABASE_MOBDROP];
	// SPLIT
	$possible_choice_arrayx = explode("|", $possible_choicex);
	// COUNT
	$possible_choice_array_totalx = count($possible_choice_arrayx);

	foreach ($_POST['checkbox'] as $key => $val) {
		echo "Checkbox: {$key} contains a value of {$val}<br />";
	}
}
}

Link to comment
Share on other sites

You have the foreach in the while. That is where you are getting the issue at. If you call $_POST['checkbox']['itemnamehere'] that will solve the problem.

 

I am not sure how you get the checkbox name value but you can use that to pull out the item you want to update. Hope that makes sense, as I would not even know where to start on your code to show you how to implement it.

Link to comment
Share on other sites

the item names runs under a for.

example:

 

$possible_choice_arrayx[$i]

 

will convert into w/e it is.

 

The possible_choicex looks like this:

item1|item2|item3|item4

 

the possible_choice_arrayx splits it in "|"

which then creates the [1][2][3] etc array.

 

I'm guessing I'd have to put the foreach in a for loop to generate the name? Maybe I can live with it for now as it'll just end up inserting the exact same information into the database.

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.