Could i use dynamic variable inside my $.post
JQuery/Ajax code? I would like to know where i am making mistakes or i am misunderstood the useage of $.post
of JQuery.
I have dynamically created variables and value assigned to it from text-fields
. Now i want to use those variables inside $.post
of JQuery.
Code Below:
The part that's working fine:
var boxFields = ["customerId","customerName","customerContact","customerEmail"];
var boxVar = [];
for(var x=0;x<4;x++)
{
boxVar[x] = $("#" + boxFields[x]).val();
}
alert(boxVar[1]); //Just random call to check it works.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="text" id="customerId" value="12345"/>
<br>
<br>
<input type="text" id="customerName" value="John Doe"/>
<br>
<br>
<input type="text" id="customerContact" value="XXXXXXXXXX"/>
<br>
<br>
<input type="text" id="customerEmail" value="xxxxxx@xxx.com"/>
Now i have dynamically generated variables (check above hidden snippet for that)
. So to use those in $.post
.
Update:
Sorry, for missing the actual part
My Problem/Question: Could i use for loop to create dynamic objects inside $.post
.
Whole Code:
var boxFields = ["customerId","customerName","customerContact","customerEmail"];
var boxVar = [];
for(var x=0;x<4;x++)
{
boxVar[x] = $("#" + boxFields[x]).val();
}
$.post("url.php",
{
requestType: "updateRow",
for(var y=0;y<4;y++)
{
boxFields[y]: boxVar[y]
if(y!=3){,}
}
/* I want above code to work like this:
requestType: "updateRow",
customerId: 12345,
customerName: John Doe,
customerContact: XXXXXXXXXX,
customerEmail: xxxxxx@xxx.com
*/
},
function(data)
{
//Success Code Lines..........
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="text" id="customerId" value="12345"/>
<br>
<br>
<input type="text" id="customerName" value="John Doe"/>
<br>
<br>
<input type="text" id="customerContact" value="XXXXXXXXXX"/>
<br>
<br>
<input type="text" id="customerEmail" value="xxxxxx@xxx.com"/>
Also would like to know how to prevent from getting SQL Injection
Thank You!
</div>