0

I'm not sure exactly how to phrase this so I will show an example. I'm gathering input values in javascript and passing to my php page where I am trying to insert those values in a database.

Instead of inserting separate values it is inserting the entire string.

Part of my javascript below:

var form = document.forms[0];
var txtS = form["bulletlabels"];
var len = txtS.length;
var bulletlabels = "";
for(i=0;i<len;i++) {
    bulletlabels += '"'+[i]+'_'+(txtS[i].value)+'_label",';
    }

when I do an alert(bulletlabels); I get this:

"0_Lot Size_label","1_Rooms_label","2_Bathrooms_label","3_Basement_label",

On my php page I have:

$bulletlabels = array($_POST['bulletlabels']);

$length = count($bulletlabels);
for ($i = 0; $i < $length; $i++) {
mysqli_query($con,"UPDATE bullets SET bullettitle = '".$bulletlabels[$i]."' WHERE bulletrow = ($i+1)");
}

This inserts the below string into the database on ONE Row which is not the desired effect:

"0_Lot Size_label","1_Rooms_label","2_Bathrooms_label","3_Basement_label",

But here is the key to my confusion - if I manually type the string in, it inserts onto individual database rows as desired.

This inserts values individually as desired when typed manually:

$bulletlabels = array("0_Lot Size_label","1_Rooms_label","2_Bathrooms_label","3_Basement_label",);

Does NOT work and inserts the full concatenated string: $bulletlabels = array($_POST['bulletlabels']);

Hope I explained well enough - arrays elude me.

EDIT:
Fix for the trailing comma:

var delim = "";
for(i=0;i<len;i++) {
bulletlabels += delim+[i]+'_'+(txtS[i].value)+'_label';
delim = ",";
}

Reference link for trailing comma fix:

Can you use a trailing comma in a JSON object?

Community
  • 1
  • 1
  • `var_dump($bulletlabels);` and you'll see that it isn't what you believed. What you can do is use proper JSON syntax and then `json_decode()` the posted string resulting in an array – kero Jun 23 '14 at 22:18
  • After you get the values from the `$_POST` array, use `print_r` or `var_dump` to see what you have. I think you'll find it's an array of strings with only one element. – Kryten Jun 23 '14 at 22:19

1 Answers1

1

Try changing the following line:

$bulletlabels = array($_POST['bulletlabels']);

to

$bulletlabels = explode(',', $_POST['bulletlabels']);

Also do not add quotes in your javascript:

 bulletlabels += '"'+[i]+'_'+(txtS[i].value)+'_label",';

should be

 bulletlabels += [i]+'_'+(txtS[i].value)+'_label,';

Explanation:

Currently, $bulletlabels is an array with one element, and this element is the following string: "0_Lot Size_label","1_Rooms_label","2_Bathrooms_label","3_Basement_label",. However, you want to have an array with several strings. That's why you need to use the explode function to convert it into a proper array.

Note:

Make sure not to include , in the label names, as it will break with this implementation. If you need to be able to use , too, you should use json functions.

Christian
  • 1,663
  • 19
  • 33
  • Sure, that won't work. Added a hint to the answer to use json in that case. – Christian Jun 23 '14 at 22:24
  • Although I believe you should use JSON anyhow instead of a workaround, you can't change OP's mind - so good answer and +1 ;) – kero Jun 23 '14 at 22:25
  • I tried your solution and took into consideration your advice on commas causing issues. Please see my edit - I found a solution for the trailing comma here - http://stackoverflow.com/questions/201782/can-you-use-a-trailing-comma-in-a-json-object But I am hoping that is acceptable coding practice as I'm uneducated of the json function you are referring to. I will mark yours as the answer and I appreciate the explanation! – user3272646 Jun 23 '14 at 23:06