简体   繁体   中英

What does the php insert code look like when dealing with ajax and serialized data?

UPDATE II

$("#form").submit(function(event){
event.preventDefault();
var $form = $( this ),
url = $form.attr( 'action' );
var posting = $.post( url, {
id: $('#id').val(),
name: $('#name').val(),
wname: $('#wname').val(),
xcor: $('#xcor').val(),
ycor: $('#ycor').val(),
xwid: $('#xwid').val(),
yhei: $('#yhei').val(),
photo: $('#photo').val(),
targeturl: $('#targeturl').val()
});

posting.done(function( data ){
alert('success');
});
});

UPDATE

This does not work... the alert('nice'); is not triggered

$("#form").submit(function(e){
var postData = $(this).seralizeArray();
var formURL = $(this).attr("action");
$.ajax({
url : formURL,
type : "POST",
data : postData
});
e.preventDefault();
e.unbind();
});
$("#form").submit();
alert('nice');

My goal is to send data with a form through ajax and the page does not refresh.

I am not using a button, the submit is triggered by a javascript function which triggers the ajax

The serialized data I had follows the format of

name=name$email=email etc...

I don't know what to do with this string

This is what I have on the sending page

var frm = $('#form');
var formData = $('#form').serialize();
frm.submit(function (ev) {
$.ajax({
type: frm.attr('method'),
data: formData,
url: frm.attr('action'),
success: function (data) {
alert('successful update');
}
});
ev.preventDefault();
});

Does the serialized data get appended to a URL? I mean I realize upon submission the current page does not go anywhere so how would that work as far as the action page "seeing the url and pulling it" so to speak...

My action page is the cliche design of a form being submitted by a button on the same page mostly it is a declaration of the variables and then an insert line

if($_SERVER['REQUEST_METHOD']=='POST'){

 $name = $_POST['name'];
 $email= $_POST['email'];

 $link = new mysqli("$servername", "$username", "$password", "$dbname");

 $stmt = mysqli_prepare($link, "INSERT INTO table VALUES(?,?)");
    $stmt->bind_param('ss',$name,$email);

        $stmt->execute();

I apologize if this is clearly wrong, I haven't used serialized form data submission before

Assuming your form method frm.attr('method') is POST, the serialized data does not get put in the URL but instead in the HTTP request body.

http://en.wikipedia.org/wiki/HTTP_message_body

If the form method is GET then the data would be appended to the end of the URL like so: http://yoursite.com/yourrequestpage?name=yourname&email=youremail

If you are using the GET method then on the PHP side, you would have to use $_GET['email'] instead of $_POST['email'] .

I also see that var formData = $('#form').serialize(); is out of the form submission function body. This should be put in the body so that it is initialized when the form is submitted (when you have filled out the form), not when the page is loaded (when you haven't yet interacted with the form).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM