dongyanjing5975
2018-05-21 00:06
浏览 773
已采纳

在AJAX中使用post传递文本框数据的正确方法

Right now I am using this, and it works perfectly, but for some reason I don't think this is the best or most efficient way to accomplish this task. It seems like it will get messy as I start to add more textbook inputs. Please review:

index.html:

function saveUserData(userData) {
  $.post('userData.php', {
    textbox1: document.getElementById('textbox1').value,
    textbox2: document.getElementById('textbox2').value, 
    userData: JSON.stringify(userData)
  }, function(data) {
    return true; 
  });

html:

<input name="textbox1" type="textbox1" id="textbox1">
<input name="textbox2" type="textbox2" id="textbox2">

userdata.php:

$userData = json_decode($_POST['userData']);
$textbox1 = $_POST['textbox1'];
$textbox2 = $_POST['textbox2'];
//Insert user data
$query = "INSERT INTO users SET first_name = '".$userData->first_name."', last_name = '".$userData->last_name."', email = '".$userData->email."', picture = '".$userData->picture->data->url."', created = '".date("Y-m-d H:i:s")."', modified = '".date("Y-m-d H:i:s")."',test1 = '".$textbox1."',test2 = '".$textbox2."' ";
$insert = $db->query($query);

图片转代码服务由CSDN问答提供 功能建议

现在我正在使用它,它工作得很好,但出于某种原因我不认为这是 完成此任务的最佳或最有效的方法。 当我开始添加更多的教科书输入时,它似乎会变得混乱。 请查看:</ p>

index.html:</ p>

  function saveUserData(userData){
 $ .post('userData.php',  {
 textbox1:document.getElementById('textbox1')。value,
 textbox2:document.getElementById('textbox2')。value,
 userData:JSON.stringify(userData)
},function(data){  
返回true; 
}); 
 </ code> </ pre> 
 
 

html:</ p>

 &lt; input name =“textbox1  “type =”textbox1“id =”textbox1“&gt; 
&lt; input name =”textbox2“type =”textbox2“id =”textbox2“&gt; 
 </ code> </ pre> 
 
 

userdata.php:</ p>

  $ userData = json_decode($ _ POST ['userData']); 
 $ textbox1 = $ _POST  ['textbox1']; 
 $ textbox2 = $ _POST ['textbox2']; 
 //插入用户数据
 $ query =“INSERT INTO users SET first_name ='”。$ userData-&gt; first_name。“'  ,last_name ='“。$ userData-&gt; last_name。”',email ='“。$ userData-&gt; email。”',picture ='“。$ userData-&gt; picture-&gt; data-&gt; url  。“',created ='”。date(“Ymd H:i:s”)。“',modified ='”。date(“Ymd H:i:s”)。“',tes  t1 ='“。$ textbox1。”',test2 ='“。$ textbox2。”'“; 
 $ insert = $ db-&gt; query($ query); 
 </ code> </ pre> \  n </ div>

1条回答 默认 最新

相关推荐 更多相似问题