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.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • make a var_dump of $_REQUEST['Tags'] you seen it was an array normaly – Inazo Aug 23 '18 at 09:44
  • value='Milk' for all four checkboxes – Dr Manish Lataa-Manohar Joshi Aug 23 '18 at 09:45
  • try changing name='Tags' to name='Tags[]', and set diferent values, you should get array of checked options – Edgarth Aug 23 '18 at 09: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 – Daniel de Ridder Aug 23 '18 at 09:54
  • @Inazo when I var_dump it, it shows [string(9) "Chocolate"] so im still under the impression it only saves the last one – Daniel de Ridder Aug 23 '18 at 09:57
  • @Edgarth Changed the name to Tags[], probably am filling it wrong now. '$Tags[] = mysqli_real_escape_string($conn, $_REQUEST['Tags']); $lenght = count ($Tags); for ($j=0;$j < $lenght; $j++){ echo "Tags[" . $j . "] = " . $Tags[$j] . "
    "; }' This just returns Tags[0] = Chocolate
    – Daniel de Ridder Aug 23 '18 at 10:02
  • Note: when adding updates to your question, they should go at the end, to keep them in the correct chronological order. Questions here are primarily kept for future readers, and those readers will always start reading from the top and work their way down, just like most written material. – halfer Aug 23 '18 at 10:54
  • 1
    I see, thank you @halfer! – Daniel de Ridder Aug 23 '18 at 11:08

3 Answers3

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.

tGilvonas
  • 205
  • 4
  • 15
  • 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? – Daniel de Ridder Aug 23 '18 at 10:06
  • 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: " https://www.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). – tGilvonas Aug 23 '18 at 10:12
  • Another good question why is it not very good practice to access PHP superglobals: https://stackoverflow.com/questions/19767894/warning-do-not-access-superglobal-post-array-directly-on-netbeans-7-4-for-ph . If you are amateur programmer, I think you should read this :) – tGilvonas Aug 23 '18 at 10:16
  • Thank you! I`ll look into this :) – Daniel de Ridder Aug 23 '18 at 10:21
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.

Chukwuemeka Inya
  • 2,575
  • 1
  • 17
  • 23
  • 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! – Daniel de Ridder Aug 23 '18 at 10:05
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'].

Chukwuemeka Inya
  • 2,575
  • 1
  • 17
  • 23