简体   繁体   English

AJAX POST无法使用document.getElementById变量

[英]AJAX POST Won't Work With document.getElementById Variables

I have an html form and ajax call that store data in MySQL via a PHP page. 我有一个HTML表单和ajax调用,可通过PHP页面将数据存储在MySQL中。

The code for all three is copied below. 这三个代码都复制到下面。 (Please note the //) (请注意//)

All three work fine as long as I have the variables hard coded in the function where the ajax call stores them. 只要我在ajax调用存储它们的函数中对变量进行了硬编码,这三个函数就可以正常工作。 However, when I comment out the hard coded variables and run it with the regular variables, it does not work. 但是,当我注释掉硬编码的变量并使用常规变量运行它时,它将不起作用。

JavaScript AJAX Call $("#buttonSubmit").click(function() { JavaScript AJAX调用$(“#buttonSubmit”)。click(function(){

//var questionID       = obj.Questions[i].questionID;
//var shortAnswerValue = document.getElementById('txtShortAnswerValue').value;      
//var longAnswerText   = document.getElementById('txtLongAnswerText').value;

var questionID      = "SampleQID";
var shortAnswerValue    = "Sample Short";       
var longAnswerText      = "Sample Long";

$.ajax({
     type: "POST",
     url: "SaveUpdatesTemplate.php",
     data: "questionID=" + questionID + "&shortAnswerValue=" + shortAnswerValue + "&longAnswerText=" + longAnswerText, 
}); // end ajax function
     document.getElementById("txtLongAnswerText").reset();  
});  // end button submit function

Associated HTML Select Inspection or Project Phase Select Inspection or Project Phase 关联的HTML选择检查或项目阶段选择检查或项目阶段

<label for="selectSection">Select Inspection or Project Phase</label>
<select class="form-control" id="selectSection" name="selectSection">
    <option> Select Inspection or Project Phase</option>
</select>   

<button type="button" class="form-control" id="buttonStart" name="buttonStart" value="List Questions">Start - Click to Populate Question List</button>
<label for="selectQuestion">Select Task or Question to Update</label>
<select class="form-control" id="selectQuestion" name="selectQuestion" >
<option> Select Task or Question to Update </option>
</select>   

<!-- short answer below -->
<label for="txtShortAnswerValue">Short Answer</label>
<select  class="form-control" id="txtShortAnswerValue" name="txtShortAnswerValue">
<option value="1" selected>worst</option>
<option value="3">middle</option>
<option value="5">best</option>
</select>

<!-- long answer below -->
<label for="txtLongAnswerText">Long Answer / Notes</label>
<textarea class="form-control" name="txtLongAnswerText" id="txtLongAnswerText" rows=3>
</textarea>

Associated PHP Code // Assign PHP variables to POST results from client 关联的PHP代码//将PHP变量分配给客户端的POST结果

$questionID         = htmlspecialchars(trim($_POST['questionID']));
$shortAnswerValue       = htmlspecialchars(trim($_POST['shortAnswerValue']));
$longAnswerText         = htmlspecialchars(trim($_POST['longAnswerText']));

   //SQL STATEMENT 
   $sql="INSERT INTO Updates (questionID, shortAnswerValue, longAnswerText)
   VALUES
   ('$questionID', '$shortAnswerValue', '$longAnswerText')";

Ajax data gets formated like this Ajax数据的格式如下

data: {questionID: questionID, shortAnswerValue: shortAnswerValue, longAnswerText: longAnswerText},

or 要么

data: {key:value, key2,"staticvalue"}

jQuery will build the string that goes onto the end of the URL. jQuery将构建URL末尾的字符串。

You could do this: 您可以这样做:

data: encodeURI("questionID=" + questionID + "&shortAnswerValue=" + shortAnswerValue + "&longAnswerText=" + longAnswerText);

The problem is the spaces in var questionID , and so on. 问题是var questionID的空格,依此类推。 Also you have an unnecessary comma before the end of the Object you passed to $.ajax() , right after your data property value. 另外,在传递给$.ajax()的Object末尾( $.ajax() data属性值之后$.ajax() ,您还有一个不必要的逗号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM