开发者

AJAX + jQuery variables do not pass to PHP

I am trying to do something very simple: Pass 2 text variables to a php script and insert them i开发者_如何学编程nto a MySQL db. For some reason however I can't get the variables to pass (so I just get empty records in my DB).

function ajaxCall(){
 
        $.ajax({
            type: "GET",
            url: "http://www.*.be/bubblingAjax.php",
            cache: false,
            data: "colour="+colour+"&size="+size,
        dataType: "html",
            success: onSuccess
        });
        return false;
    };

And the PHP:

<?php
    try
    {
        $connection = mysql_connect("#");
        mysql_select_db("#");

        $colour = mysql_real_escape_string($_GET['colour']);
        $size = mysql_real_escape_string($_GET['size']);
        
        mysql_query("INSERT INTO bubble (colour, size) VALUES ('$colour', '$size')");
        mysql_close($connection);
        echo "SUCCESS";
        echo $colour;
        echo $size;
    }
    catch(Exception $e)
    {
        echo $e->getMessage();
    }
?>

Anyone willing to take a quick look at it and point out my -probably obvious- mistake? It's been driving me nuts for over a day.


This has to work:

<script type="text/javascript">
$(document).ready(function() {
   //you can wrap the code into an event, e.g click()
   var colour=...
   var size=...
   $.post("http://www.website.com/bubblingajax.php", { colour: colour, size: size },
   function(data) {
     alert("Respond: " + data);
   }); 
});

</script>

and the PHP (only changed get to post)

<?php
    try
    {
        $connection = mysql_connect("#");
        mysql_select_db("#");

        $colour = mysql_real_escape_string($_POST['colour']);
        $size = mysql_real_escape_string($_POST['size']);

        mysql_query("INSERT INTO bubble (colour, size) VALUES ('$colour', '$size')");
        mysql_close($connection);
        echo "SUCCESS";
        echo $colour;
        echo $size;
    }
    catch(Exception $e)
    {
        echo $e->getMessage();
    }
?>

Also to debug, I would suggest using firebug or chrome's build in inspect tools.


The "data" parameters are sent as POST varaiables rather than GET variables. Try $_POST in the PHP


TEST THIS IN a test.php file (name matters):

<?PHP 
if(isset($_POST['user_name']))
{ 
$post_output=
'Hello '.strtoupper($_POST['user_name']).' from '.strtoupper($_POST['user_city']).'!

This is an other random:'.rand(23,46).'.

The previous random is still alive!

I guess you can insert these 2 values 
in the database on your own now, don\'t you?!';


$get_output=
'


___________________________________________
Well if you insist you can keep using get on parallel
This is what $_GET says:

'.strtoupper($_GET['getMessage']).'

And finally you can avoid post at all, to do that:
1.Use get instead of post inside the insertToDB function
2.Use send(null) instead of send(params)
3.Don\'t send the headers

HOWEVER I LIKE POST!';
echo $post_output;
print $get_output;
exit;
}
?>

<html>

<head>

<script language="javascript" type="text/javascript"  >
<!--

var request = false;
try { 
  request = new XMLHttpRequest(); 
} catch (trymicrosoft) {                         
  try { 
    request = new ActiveXObject("Msxml2.XMLHTTP"); 
  } catch (othermicrosoft) {
    try {
      request = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (failed) {                  
      request = false;       
    }
  }
}

if (!request) 
  alert("Error initializing XMLHttpRequest!"); 

   function insertToDB() 
   { 
        var url = "test.php?getMessage=Hi%20There!%20Use%20me%20if%20you%20like..";
        var params = "user_name=" +  (document.getElementById("user_name").value)+ 
        "&user_city="+(document.getElementById("user_city").value);

        request.open("POST", url, true); 

        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
        request.setRequestHeader("Content-length", params.length);
        request.setRequestHeader("Connection", "close");

        request.onreadystatechange = updatePage;
        request.send(params);

   }////////////////////

   //You're looking for a status code of 200 which simply means okay.
   function updatePage() {
     if (request.readyState == 4) {
       if (request.status == 200) 
       { 
       //alert(decodeURIComponent(request.responseText));

             alert(request.responseText);
       } 
       else{
         //alert("status is " + request.status);
         }
     }
   }


// -->
</script>
</head>

<html>
<body>

This is a random:<?PHP echo rand(1,23);?><br>


<input type="text" name="name" value="your name here"  size=80 id='user_name'   ><br>
<input type="text" name="city" value="your city goes here"  size=80  id='user_city'  ><br>
<input type="submit" name="bttn" value="Go" onClick="insertToDB( );" > 




</body>
</html>


running on a mobile phone

Well I don't know if my script above will do in a mobile phone, it needs Javascript enabled!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜