0

I have a form to upload items to my database. In this form I have a selection of checkboxes to give tags to the items. preferably I want to save these tags as one single string in my database, separated by a comma. However, no matter what I try, I only seem to get an error or just the final result of my checkboxes.

<form action="storeupdate.php" enctype="multipart/form-data" method="post">
<input type='checkbox' name='Tags' value='Milk' >Milk<br/>
<input type='checkbox' name='Tags' value='Sugar' >Sugar<br/>
<input type='checkbox' name='Tags' value='Cream' >Cream<br/>
<input type='checkbox' name='Tags' value='Chocolate' >Chocolate<br/>
<input type="reset"> <input type="submit" name="Submit" value="Submit"><br/>
</form>

storeupdate.php:

$Tags = mysqli_real_escape_string($conn, $_REQUEST['Tags']);

I have tried foreach loops, but they dont seem to give me any results back. When I echo the $Tags, I only get the 'highest' result that has been submitted.

If I can't even figure out how to grab the individual values, I can't even begin to work on a script to string them together before placing them in my database.

Update

Now that we have established I did wrong in the form, I am still confused in how to catch and convert this array.

$Tags[] = mysqli_real_escape_string($conn, $_POST['Tags[]']);
$tagstring = implode(",",$Tags);
echo "<br/> $_REQUEST = ";
var_dump($_POST['Tags']);
echo "<br/> $Tags = ";
var_dump($Tags);
echo "<br/> $tagstring = ";
var_dump($tagstring);
echo "<br/>";
echo "Tags= " . $tagstring . "<br/>";

returns the following: Array = array(6) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" [5]=> string(1) "6" } Array = array(1) { [0]=> string(0) "" } = string(0) "" Tags=

I need this array as a string separated by the comma in order to make the rest of my build code work, but im doing something wrong in the handling after I catch the data.

8
  • make a var_dump of $_REQUEST['Tags'] you seen it was an array normaly Commented Aug 23, 2018 at 9:44
  • value='Milk' for all four checkboxes Commented Aug 23, 2018 at 9:45
  • try changing name='Tags' to name='Tags[]', and set diferent values, you should get array of checked options Commented Aug 23, 2018 at 9:47
  • Shoot, i cleaned out the original values from the project file because my colleague doesnt want me to share that info. It shouldnt say "milk" in every value, my bad haha! @DrMJ Commented Aug 23, 2018 at 9:54
  • 1
    I see, thank you @halfer! Commented Aug 23, 2018 at 11:08

3 Answers 3

1

You should write such HTML:

<form action="storeupdate.php" enctype="multipart/form-data" method="post">
<input type='checkbox' name='Tags[]' value='Milk' >Milk<br/>
<input type='checkbox' name='Tags[]' value='Sugar' >Sugar<br/>
<input type='checkbox' name='Tags[]' value='Cream' >Cream<br/>
<input type='checkbox' name='Tags[]' value='Chocolate' >Chocolate<br/>
<input type="reset"> <input type="submit" name="Submit" value="Submit"><br/>
</form>

And take $_POST['Tags'] from $_POST request where $_POST['Tags'] is array of your tags.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your feedback @tGilvonas. However, I never understood what the advantage of $_POST would be over $_REQUEST, or what their differences are. Should I prefer one over the other in this scenario?
As some sources say, "$REQUEST contains: $COOKIE, $GET, and $POST variables" and "Basically... never use $REQUEST, use $POST for post method forms, $GET for query string and get method forms, and $COOKIE to handle cookies." link here: " webdeveloper.com/forum/d/71478-post-vs-request ". Sorry, I think I have weak knowledge on this question, but I never used $_REQUEST since I write codes in PHP (2008).
Another good question why is it not very good practice to access PHP superglobals: stackoverflow.com/questions/19767894/… . If you are amateur programmer, I think you should read this :)
Thank you! I`ll look into this :)
0

If you want to have an array of Tags you need to declare this parameter as an array in HTML:

<input type='checkbox' name='tags[]' value='Milk' >Milk<br/>
<input type='checkbox' name='tags[]' value='Sugar' >Sugar<br/>
<input type='checkbox' name='tags[]' value='Cream' >Cream<br/>
<input type='checkbox' name='tags[]' value='Chocolate' >Chocolate<br/>

Now $_REQUEST['tags'] will contain an array.

1 Comment

Ah wonderful, I misunderstood @Edgarth earlier when he tried to tell me the same. Now I just need to convert this array into a string and I should be set! Thank you Damian!
0
<form action="storeupdate.php" method="post">             //enctype="multipart/form-data" The "multipart/form-data" is not required unless you are uploading a file. otherwise no need to set it. It has a default value of "application/x-www-form-urlencoded"
    <input type='checkbox' name='tags[]' value='Milk' >Milk<br/>             //Try to use small letters, hyphen and underscore when setting the name attribute of your form.
    <input type='checkbox' name='tags[]' value='Sugar' >Sugar<br/>
    <input type='checkbox' name='tags[]' value='Cream' >Cream<br/>
    <input type='checkbox' name='tags[]' value='Chocolate' >Chocolate<br/>
    <input type="reset"> <input type="submit" name="submit" value="Submit"><br/>
</form>

Now your data will be in $_POST['tags'].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.