form data into database using jQuery

I have a function in JavaScript with the following parameters below:

insertdata(micheal, newyork)

I want to be able to insert thus:

name = micheal
location = newyork 

into my MySQL database using PHP and jQuery.

Both items of data are not coming from a form. I'm only passing them from JavaScript.


Since I cannot comment due to reputation, I suggest you a solution that through jQuery you can append this to your current form as hidden fields and these get submitted at the PHP backend and you can save it in your DB.

Let me know if you need help on how to append it to the form.

var name = $("<input type='hidden' name='whatever' value='micheal' />");
$("#myform").append(name);

Also have a look at this

Add elements using jQuery dynamically

To process this in PHP see this,

$variable_name_1 = $_POST['name_of_form_field_1'];
$variable_name_2 = $_POST['name_of_form_field_2'];

//Read how to sanitize variables
$query = "INSERT INTO table_name (field1, field2) VALUES ('$variable_name_1', '$variable_name_2')";
$result = mysqli_query($query);
if(!$result) {
   echo "Error" . mysqli_error();
}
else {
  echo "Done!";
}

You can have a snapshot of your form submitted by doing this var_dump($_POST)

链接地址: http://www.djcxy.com/p/83500.html

上一篇: 如何循环使用相同类名的每个图像?

下一篇: 使用jQuery将数据形成数据库