Jump to content

Storing Into Array by Checkbox?


glassfish

Recommended Posts

Script:

<?php

// For example, the entries in the table.
$entry1 = "text1";
$entry2 = "text2";
$entry3 = "text3";

// For example, the ID numbers in the table.
$value1 = "140";
$value2 = "141";
$value3 = "142";

echo "<form method='POST' action='" . $_SERVER['PHP_SELF'] . "'>";
echo "<input type='checkbox' name='hashtag_modify_checkbox[]' value='" . $value1 . "' />";
echo "Modify";
echo "<input type='text' name='hashtag_name[]' value='" . $entry1 . "' />";

echo "<input type='checkbox' name='hashtag_modify_checkbox[]' value='" . $value2 . "' />";
echo "Modify";
echo "<input type='text' name='hashtag_name[]' value='" . $entry2 . "' />";

echo "<input type='checkbox' name='hashtag_modify_checkbox[]' value='" . $value3 . "' />";
echo "Modify";
echo "<input type='text' name='hashtag_name[]' value='" . $entry3 . "' />";

echo "<input type='submit' name='submit' />";
echo "</form>";


// Here, only store those hashtag names where the "modify" checkbox is checked.


?>

This is used for the admin panel.

 

It is a form where the input fields hold the text ("hashtags") already inside of them.

 

Basically, one would have to check the "modify checkbox" to modify a hashtag, then only the modified hashtags should get stored into an array.

 

How to have those hashtags stored in the array with the checked checkbox as the factor?

 

The suggestions are much appreciated.

Edited by glassfish
Link to comment
Share on other sites

It is used for a SQL update query, if I would not do this check all of the "entries" would get updated in the table (with the "for" loop), even though no modifications have been. Does this clarify?

 

So basically, "check the checkbox", "modify the entry", and then have only those in the table updated where the modification has been done (where the checkbox is checked).

Link to comment
Share on other sites

That setup doesn't make sense. Checkboxes are not sent in the post data if they are not checked. So, you need to associate the checkboxes with the text boxes. I would make the text boxes an array using the entry name as the index and then use the entry name as the value for the checkboxes.

 

 

echo "<form method='POST' action='{$_SERVER['PHP_SELF']}'>\n";
 
echo "<input type='checkbox' name='hashtag_modify[]' value='{$entry1}' /> Modify\n";
echo "<input type='text' name='hashtag_name[{entry1}]' value='{$value1}' />\n";
 
echo "<input type='checkbox' name='hashtag_modify[]' value='{$entry2}' />Modify\n";
echo "<input type='text' name='hashtag_name[{$entry2}]' value='{$value2}' />\n";
 
echo "<input type='checkbox' name='hashtag_modify[]' value='{$entry3}' />Modify\n";
echo "<input type='text' name='hashtag_name[{$entry3}]' value='{$value3}' />\n";
 
echo "<input type='submit' name='submit' />\n";
echo "</form>\n";

 

Now, you can determine which textboxes to process using the array of checkbox values.

 

 

foreach($_POST['hashtag_modify'] as $entry)
{
    echo "<br>Name of entry to be modified: " . $entry;
    echo "<br>New value for the entry: " . $_POST['hashtag_value'][$entry];
}
Link to comment
Share on other sites

First of all, you need to start thinking about security. You can't just drop client-controlled values like $_SERVER['PHP_SELF'] into your HTML markup, because this allows anybody to inject JavaScript code and attack your users. Every value you want to insert into an HTML context must be escaped with htmlspecialchars().

 

Regarding your original question, use explicit indexes to assign the checkboxes to the text fields. For example:

<input type="text" name="hashtag[0][content]">
<input type="checkbox" name="hashtag[0][update]">

Of course you'd use a loop and a counter to number the fields.

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.