Jump to content

Post to PHP with jQuery


mrbambrick

Recommended Posts

Hello All,
 
I have the following jQuery script that I would like to post to my mysql database with php. How would I go about getting this information to post to the database? Here is what I have so far.
 
Thank you in advance.
 

 

<script>

$(document).ready(function () {
 
//we want to intialze the values
values = {};
values['vehicle'] = "car";
values['gender'] = "male";
values['age'] = "25";
values['position'] = "manager";
 
//we need to handle the image clicks
$("#car, #bus, #train, #boat, #plane, #man, #woman, #25, #35, #45, #55, #manager, #director, #president, #CXO").click(function (event) {
event.preventDefault();
 
//we want to know what row we are dealing with
var imageclass = $(this).attr('class');
 
//we need all of the other images in the row turned to not selected
$("." + imageclass).each(function() {
if ($(this).id != event.target.id)
{
var src = $(this).attr('src');
if (src.indexOf("2") >= 0)
{
$(this).attr('src', src.replace("2.png", ".png"));
}
}
});
 
setImageAsSelected(event.target.id);
});
 
function setImageAsSelected(id) {
var current = $("#" + id).attr('src');
var next;
if (current.indexOf("2") < 0) {
next = current.replace(".png", "2.png");
}
$("#" + id).attr('src', next);
values[$("#" + id).attr('class')] = id;
}
 
//we need to handle the go click
$("#go").click(function () {
var sql = "INSERT INTO TABLE (Vehicle, Gender, Age, Positon) VALUES ('" + values['vehicle'] + "', '" + values['gender'] + "', '" + values['age'] + "','" + values['position'] + "');";
alert(sql);
});
 
});
 
</script>

Link to comment
Share on other sites

Ummm, well it shows you exactly how to use jquery to send data to a php script for processing.  What is your updated code after watching the video and implementing the post/ajax request and what is your php code for the db insert? You're not really telling us anything by saying it still doesn't work.  Also it's not a good thing to place the sql string in the jquery code for the world to see.  Especially if you are literally taking that sql string and passing it to a query command.  Literally anyone could change that sql string and do literally anything they wanted on your server, very very very bad thing.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.