1

This is my ajax form which works,

$.ajax({
type: "POST",
url: "update_coordinate.php",
data: 
'id=' +$id+
'name=' +$name+
'wname=' +$wname+
'xcor=' +$xcor+
'ycor=' +$ycor+
'xwid=' +$xwid+
'yhei=' +$yhei+
'photo=' +$photo+
'targeturl=' +$targeturl,
success: function(data){
alert(
' id:' +$id+
' name:' +$name+
' wname:' +$wname+
' xcor:' +$xcor+
' ycor:' +$ycor+
' xwid:' +$xwid+
' yhei:' +$yhei+
' photo:' +$photo+
' targeturl:' +$targeturl
);
alert(data);
}
});

the alert shows all of the data, the problem is the php side which doesn't seem to read the value, I'm not sure if I am missing something.

This is interesting: While messing around, I somehow got the entire concatenated string to be entered in the name field with the equals sign included... how could that have happened?

I took out the if(post) argument which is used when submitting an html form to the same page with method post and a submit button. I don't know if that is bad.

I tried both of these

if(isset($_POST['id'])){
 $id = $_POST['id'];
 }else {
 $id = "";
 }

 $name = $_POST['name'];

Getting errors Undefined index: name which the rest are fine because I specify an arbitrary string value of empty

What am I missing?

pearlescentcrescent
  • 143
  • 1
  • 3
  • 10
  • 1
    I would guess for some reason your POST is actually getting sent as a GET when it reaches the server... I would first suggest changing your PHP $_POST to $_REQUEST... or try something like print_r($_REQUEST); as this will show both POST & GET requests... For this reason i personally use $.post as the ajax object can be a little anoying to debug. – Angry 84 Feb 27 '15 at 00:50
  • 1
    Also, the way your building your data string... Should this not be a query string syntax.. as in var1=bla&var2=bla&var3=and_so_on.. i'll post a jquery post example and see if that works – Angry 84 Feb 27 '15 at 00:53
  • I printed the $_REQUEST and id is in brackets, I realize brackets generally means array but the string id= is not shown unlike the others which show name=wordwname=word etc... – pearlescentcrescent Feb 27 '15 at 00:59
  • I tried serialized which formats the data as you mentioned. I've tried like six different times on how to get this to work, six different methods and this link here is what brought me to my current structure http://stackoverflow.com/questions/17887098/two-post-requests-one-after-the-other-second-post-request-doesnt-get-execut/17887642#17887642 – pearlescentcrescent Feb 27 '15 at 01:00
  • 1
    Your `update_coordinate.php` script has an error in it. – Steve Robbins Feb 27 '15 at 01:04
  • 1
    @steve, the PHP error is generated by the data being sent from JS.. so the PHP does not have an error.. But being its dynamic from the JS, it can produce an error ... – Angry 84 Feb 27 '15 at 01:12
  • 1
    @pearlescentcrescent the link you listed above, that one is using a query string and you will notice the & symbol after every variable=value combination. So that forms a GET type request.. – Angry 84 Feb 27 '15 at 01:13
  • Everything is A-okay thus far man, thanks a lot!!! – pearlescentcrescent Feb 27 '15 at 01:16
  • 1
    Funny these little +1's are meaningless you know, as developers, they are just data, numbers, yet it has a psychological-reward-system effect... green is good, red is bad... funny... – pearlescentcrescent Feb 27 '15 at 01:17
  • 1
    for me is simply a case of.. the more +++'s the more likely the comment / answer will actually be of interest to me – Angry 84 Feb 27 '15 at 01:27
  • well thanks a lot man this was huge – pearlescentcrescent Feb 27 '15 at 01:52

2 Answers2

1

Using jQuery Post:

$.post( "update_coordinate.php", { 
    'id':$id,
    'name':$name,
    'wname':$wname,
    'xcor':$xcor,
    'ycor':$ycor,
    'xwid':$xwid,
    'yhei':$yhei,
    'photo':$photo,
    'targeturl':$targeturl
}).done(function( data ) {
    alert( "Server Response: " + data );
});

as this will send the POST data correctly, as every time i tried using ajax and having a data string, it seem to always get sent as a GET due to the server thinking it was a url / query string.

Also, by doing the above.. You can then easily send a JS array to PHP or other objects.

Angry 84
  • 2,935
  • 1
  • 25
  • 24
  • OH MY GOD!!! (I'm not religious) Thank you sir!!! Very much! I have been stuck on this problem for the last 12 hours or so. Now onto the next headache. – pearlescentcrescent Feb 27 '15 at 01:04
  • Glad i could be of help, and yep coding is always gonna be a pain in the neck on every little bug.. But i guess its why we go insane so easily these days – Angry 84 Feb 27 '15 at 01:09
1

what you are missing is how to format JSON. The "+" operator is concatenating everything as one string. JSON is formatted with {propertyname} : {value}

$.ajax({
   type: "POST",
   url: "update_coordinate.php",
   data: {
           'id' : $id,
           'name': $name,
           'wname': $wname,
           'xcor': $xcor,
           'ycor': $ycor,
           'xwid': $xwid,
           'yhei': $yhei,
           'photo': $photo,
           'targeturl': $targeturl
   }, 
   success: function(data){
     alert(
       ' id' +$id+
       ' name:' +$name+
       ' wname:' +$wname+
       ' xcor:' +$xcor+
       ' ycor:' +$ycor+
       ' xwid:' +$xwid+
       ' yhei:' +$yhei+
       ' photo:' +$photo+
       ' targeturl:' +$targeturl
 );
 alert(data);
 }

});

Crowcoder
  • 11,250
  • 3
  • 36
  • 45
  • Thank you for your explanation, I think in due time I will come across the need to use JSON, hell this thing I'm working on now seems suicidal, if 1000 people are moving their widgets around and every time the widget stops the current coordinate is updated along with current size and current function... wow... is that dumb? I don't know but thank you for your response. Stack overflow is phenomenal for resources eg. userbase. Thank you. – pearlescentcrescent Feb 27 '15 at 01:05