glassfish Posted October 23, 2014 Share Posted October 23, 2014 (edited) 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 October 23, 2014 by glassfish Quote Link to comment Share on other sites More sharing options...
MDCode Posted October 23, 2014 Share Posted October 23, 2014 I'm not exactly sure what you're going for. You want a PHP array made when they click the checkboxes? Or are you trying to make an array of already modified ones? Quote Link to comment Share on other sites More sharing options...
glassfish Posted October 23, 2014 Author Share Posted October 23, 2014 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). Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 23, 2014 Share Posted October 23, 2014 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]; } Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted October 23, 2014 Share Posted October 23, 2014 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.