Thanks for taking a look : )
I know that I can add multiple objects at once in a javascript array like this:
<script>
var myCarsArray = ["Saab","Volvo","BMW"];
alert("The second element in my array = "+myCarsArray[1]);
</script>
This alerts 'The second element in my array = Volvo'.
I want to simply store the array values in the database as a string - so in the database would be a field called 'My_Cars_Array_Database_String_Value' and I will place my array values there the exact same way I would place them in the array with javascript - so the string would look like this:
"Saab","Volvo","BMW"
Now I get the string value using MySQL and echo the value using PHP, like this
<script>
var myCarsArray = [<?php echo $My_Cars_Array_Database_String_Value; ?>];
alert("The second element in my array = "+myCarsArray[1]);
</script>
This alerts 'The second element in my array = S' !? It's treating the whole string as an array.
So the only way I could get this to work was to use 'eval' this way :
<script>
var myCarsArray = eval("["+<?php echo $My_Cars_Array_Database_String_Value; ?>+"]");
alert("The second element in my array = "+myCarsArray[1]);
</script>
This alerts correctly 'The second element in my array = Volvo'.
My question is: How do I save the string to the database so that I can just insert it into the javascript array as is, using:
myCarsArray = [<?php echo $My_Cars_Array_Database_String_Value; ?>];
?
WITHOUT using a loop of any kind - I understand how to achieve this with a loop - I don't want to use a loop. Or is the 'eval' method the only way?
Clarification:
The question on this post is how to save javascript usable array values - as a string to MySQL - that can later be easily inserted into an array via -
myCarsArray = [<?php echo $My_Cars_Array_Database_String_Value; ?>];
method.
AND Can this be done without using loops or the 'eval' statement?
Thanks in advance - I look forward to your replies.
Regards, Ken