AJAX + jQuery ......变量没有传递给PHP

时间:2011-05-29 10:57:07

标签: php jquery ajax

嘿伙计们, 尝试做一些非常简单的事情:将2个文本变量传递给php脚本并将它们插入到MySQL数据库中。但由于某些原因,我无法通过变量(所以我只在数据库中获取空记录)。

function ajaxCall(){

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

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();
    }
?>

任何人都愿意快速查看并指出我可能是明显的错误吗?这让我疯了一天多了!

谢谢!

4 个答案:

答案 0 :(得分:0)

“data”参数作为POST varaiables而不是GET变量发送。 在PHP中尝试$ _POST

答案 1 :(得分:0)

这必须起作用:

<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>

和PHP(只改变了发布)

<?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();
    }
?>

另外,为了调试,我建议在检查工具中使用firebug或chrome的构建。

答案 2 :(得分:0)

在test.php文件中测试(名称很重要):

<?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>

答案 3 :(得分:0)

  

在手机上运行

好吧,我不知道上面的脚本是否会在手机中完成,需要启用Javascript!