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

在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);
  • 写回答

1条回答 默认 最新

  • dqdpz60048 2018-05-21 00:31
    关注

    EDIT: Ok, how about something like this?

    //javascript
    var toPost = {};
    $("input").each(function() {
        toPost[$(this).attr('name')] = $(this).val()
    });
    
    var inputs = JSON.stringify(toPost);
    //send postJSON to php with $.post()
    
    //in php
    $inputs = json_decode($_POST["inputs"], true);
    //now all the inputs can be accessed
    foreach ($inputs as $name => $val) {
         echo "input with name '$name' has value $val";
         //you can do all you prepared statements and stuff with these values.
    }
    

    Much more general and will automatically convert your inputs to a json object with the name of the input used as the key. Don't use $(":input") as this will include things like buttons.

    --- OLD ANSWER: ---

    Can't really test this right now, but could you do something like:

    //javascript:
    var i = 1;
    while ($('#textbox'+i).length != 0) {
        postObject['textbox'+i] = $('#textbox'+i).val();
        i++;
    }
    //postObject values are then added to the $.post call
    
    //php:
    $i = 1;
    $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")."'";
    while (isset($_POST["textbox$i"])) {
        $query .= ", test$i = '".$_POST["textbox$i"]."'";
        $i++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。