2011-08-05 35 views
0

在这个页面,这个代码是,有3个细节 国家,性别一种形式,主题xmlHttp2功能(编码需要VAR PARAMS!)

这样的想法是将这些细节3送startChat.php并使php可以提取3个细节。

的代码如下

function startChat() 
      { 
      xmlHttp2 = GetXmlHttpObject(); 

      if (xmlHttp2 == null) 
       { 
       alert("Browser does not support HTTP Request"); 
       return; 
       } 

      var url = "startChat.php"; 
      var params = "country,gender,topic";<<<<<<<<<<<<<<<<<<<<<<<what coding this should be????? 
      xmlHttp2.open("GET", url, true); 
      xmlHttp2.send(params);<<<<<<<<is this correct????? 
      xmlHttp2.onreadystatechange = stateChanged2;    
      } 

,也是我需要帮助与startChat.php部分

<?php 

include('config.inc.php'); 
$preference="$_GET[params]";<<<<<<<<<<<<<<<<<<<<<<<<<<<<what coding this should be???????????????????????????????????? 

include('database.inc.php'); 
mysql_query("INSERT INTO users (inchat,preference) values('N','$preference')"); 

echo mysql_insert_id(); 

mysql_close($con); 

?> 

请帮帮忙,要真诚:(

回答

1

首先,你应该使用POST请求而不是GET,因为从代码中可以清楚地看到这个请求应该改变服务器上的状态。

您的params变量应该以表格编码。你可以用encodeURIComponent做到这一点,就像这样:

var params = 'country=' + encodeURIComponent(userCountry) + 
      '&gender=' + encodeURIComponent(userGender) + 
      '&topic=' + encodeURIComponent(userTopic); 

其次,你应该将其插入到你的数据库之前清空数据。否则,你会暴露自己的SQL注入攻击。

<

?php 

include('config.inc.php'); 

// need to create db connection before mysql_real_escape_string is called 
include('database.inc.php'); 

$country = mysql_real_escape_string($_POST['country'], $con); 
$gender = mysql_real_escape_string($_POST['gender'], $con); 
$topic = mysql_real_escape_string($_POST['topic'], $con); 

mysql_query(" 
    INSERT INTO users(inchat, country, gender, topic) 
    VALUES('N','$country', '$gender', '$topic') 
"); 

echo mysql_insert_id(); 

mysql_close($con); 

?> 

请注意,我也改变了你的数据库结构。一般来说,最好避免将多个数据放入一个字段(DB normalization)。

+0

嗨汤姆,thnx的答案,但似乎我有另一个问题,因为你看到最初的代码没有var params和发送参数,所以我认为它影响了代码部分,其中xmlHttp2.onreadystatechange = stateChanged2; }? – mp89

+0

我不认为我理解你的新问题。它看起来好像你的请求代码会通过参数(尽管这些值的来源并不明显)。我明确地从PHP脚本中的'POST'数据中读取这些值。所以看起来数据路径是从客户端到服务器完成的。您需要做的只是从客户端获取这些值 - 通过表单字段,提示,地理位置数据或您计划获得的值。这部分很容易。 – Tom

+0

我有一个下拉列表来选择所有这3个值,并为他们,国家,性别,主题设置差异名称,而不像传统的提交按钮,我使用此代码来传递值startChat.php和一些系列新的问题是,可能会有任何更改u建议干涉xmlHttp2.onreadystatechange函数,因为我知道它是一种函数来检测以前执行的成功行为,或者如果您可能请再次查看代码。 。感谢很多 – mp89