2011-08-24 87 views
0

我已阅读过类似的主题,但无法完成我的代码。插入表格行数据onclick复选框PHP和Ajax和MySQL

简而言之:您勾选了一个复选框,并取消了调用函数的Ajax响应。该功能获取登录用户的ID。一旦我获得用户的ID,我就可以使用PHP脚本。

我已经重写了代码片段;还没有尝试过。可以说这是我希望更接近。

我使用2个脚本,1个用于Ajax,另一个用于PHP。

阿贾克斯:

<html> 
    <head> 
    <script type="text/javascript"> 
    function getLoggedInUser() 
    { 

//IF THERE IS NOTHING CHECKED, THAT IS, STR == 0, THEN REMOVE WHATEVER WAS WRITTEN BEFORE 

    if (str=="") 
     { 
     document.getElementById("txtHint").innerHTML=""; 
     return; 
     } 



    // JUST CHECKING BROWSER COMPATIBILITY ISSUES FIRST 

      if (window.XMLHttpRequest) 
      {// code for IE7+, Firefox, Chrome, Opera, Safari 
       xmlhttp=new XMLHttpRequest(); 
      } 


      else 

      {// code for IE6, IE5 
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
      } 


// IF THE SERVER HAS THE RESPONSE READY 

      xmlhttp.onreadystatechange=function() 
      { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 

    document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 


     } 
     } 

THEN在具有选中该复选框将其发送到GETLOGGED在FN 用户ID,而这个函数将其转发到名为 INSERT用户数据的PHP脚本,这将JUST DO那。

xmlhttp.open("GET","insertUserData.php?$user="+str,true); 


    xmlhttp.send(); 
    } 


</script> 
</head> 


<body> 


<?php $user = getUserId();?> 

<form> 
<input type="checkbox" name="asset" value="" onclick="getLoggedInUser($user)" /> I am potentially 

interested in this product or service<br /> 
<div id="txtHint"><b>Note:</b></div> 


</form> 
<br /> 

</body> 
</html> 

======这里的PHP脚本insertUserData.php ===========

<?php 

$user = $_GET['user']; 


    // But now, as I indicated, we want the data corresponding to that ID 



    $con = mysql_connect('localhost', 'peter', 'abc123'); 
     if (!$con) 
      { 
      die('Could not connect: ' . mysql_error()); 
      } 

     mysql_select_db("ajax_demo", $con); 


     $query = "SELECT name, country 
        FROM profiles 
        WHERE id = '". $user . "'; 

     $result = @mysql_query ($sql); 

     $row = mysql_fetch_array($result, MYSQL_ASSOC); 

     $name = ($row['name']); 
     $country = ($row['country']); 

     mysql_query("INSERT INTO Persons (name, country) 
     VALUES ($name, $country)"); 
     echo 'Your interest has been saved'; 

     mysql_close($con); 
     ?> 

预先感谢您

+1

你的问题到底是什么?正如你上面已经看到的那样,你的PHP代码中存在解析错误(你没有正确关闭查询字符串引号),并且你在第一个代码片段中似乎混合了PHP和Javascript。 – Rijk

+0

看来你运行的mysql_query之前的mysql_connect –

+0

我的建议是通过一些基本的PHP/MySQL和Javascript教程工作,并更动的代码示例一点点。你会很快得到它的窍门。 – Rijk

回答

0

至少这:

$user = getUserId(); 
xmlhttp.open("GET","insertUserData.php?$user="true); 

应改为:

<? $user = getUserId(); ?> 
xmlhttp.open("GET","insertUserData.php?user=$user", true); 

这不是一个直接回答你的问题,但至少有一个起点,并且确保你通过在你的php文件的顶部添加下面的代码来启用错误。

ini_set('display_errors', 1); 
error_reporting(E_ALL); 
+0

“我得到了我的A +在我的哈佛大学甲骨文班与Dr.Pappadopoulos“,你为什么不给Pappadopoulos博士打个电话? –

+0

自从我在这里发帖后,我提到了我自己的网站,尽管使用了catcha验证码,我仍然在其中进行一系列自动垃圾邮件注册。我正在记录所有IP,并与ISP一起进行处理。 – retarded

+0

你确实被推迟了。 –