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
https://forums.phpfreaks.com/topic/288041-post-to-php-with-jquery/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.