在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);
查看全部
dongyanjing5975
dongyanjing5975
2018/05/21 00:06
  • post
  • php
  • ajax
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复