javascript - How to send data as an object through .post() in jQuery? -


when make .post() request such as

var data = $(this).serialize(); $('form').on('submit', function(event) {     event.preventdefault();     $.post('server.php', data, function(data) {         $('#data').append('<p>' + data + '</p>');     }); }); 

everything's working - name database appends #data element withing p tags. but, when try pass data object this

var data = $(this).serialize(); $.post('server.php', {id: data}, function(data) {     $('#data').append('<p>' + data + '</p>'); }); 

than doesn't work. tried changing argument of function within .post() id , every combination of names .post()'s arguments , variables within php file also, without success. here's working intact php file compatible first version of .post() request in question:

<?php      $id = $_post['id'];      $connection = new mysqli('localhost', 'root', '', 'dummy_db');      $query = 'select name dummy_db_table id = "' . $id . '"';     $result = $connection->query($query);     $row = $result->fetch_assoc();      echo $row["name"];      $connection->close();  ?> 

please note name of input field id in html file 'id'. understand name attribute within html helps php determine value of it, how doing without me specifying connection php through form's action attribute? i'm doing exclusively through ajax (.post()) , ajax not telling php specific id field. missing here? also, how go sending object of values instead of single 1 through .post()'s data attribute? thank you.

you have not add form code here lets assume form have 2 fields name , address. need put serialize() function under event. like

$('form').on('submit', function(event) {     event.preventdefault();     var data = $(this).serialize();     $.post('server.php', {id: data}, function(data) {         $('#data').append('<p>' + data + '</p>');     }); }); 

now on server.php file if print following line:

$id = $_post['id']; echo $id; 

this show results like: name=iffi&address=uk

i hope more.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -