无法使用AJAX发送POST值

时间:2019-02-25 10:05:43

标签: php html mysql ajax post

我有两个单独的文件,一个是bargraph.html,另一个是data.php。

Bargraph.html如下:

<form method="POST" name="dataform" action="">
  <select name="data1" id="data1-value">
    <option value="DateRecorded">DateRecorded</option>
    <option value="InletVoltage">InletVoltage</option>
    <option value="InletCurrent">InletCurrent</option>
    <option value="ActivePower">ActivePower</option>
    <option value="PowerFactor">PowerFactor</option>
    <option value="SystemID">SystemID</option>
    </select>
    <select name="data2" id ="data2-value">
    <option value="DateRecorded">DateRecorded</option>
    <option value="InletVoltage">InletVoltage</option>
    <option value="InletCurrent">InletCurrent</option>
    <option value="ActivePower">ActivePower</option>
    <option value="PowerFactor">PowerFactor</option>
    <option value="SystemID">SystemID</option>
  </select>
</form>

<script type="text/javascript">

$('#data1-value').change(function(){
        var data1Value = $(this).val();
$('#data2-value').change(function(){
        var data2Value = $(this).val();

    $.ajax({
        type: 'post',
        url: 'data.php',
        dataType: 'html',
        data: {data1, data2: data1Value, data2Value},
        success:function(data){ 
            alert(data); 
        }, 
        error:function (xhr, ajaxOptions, thrownError){
            alert(thrownError);
        }, 
        complete: function(){
        }
    });
})});
</script>

data.php的一部分如下:

if (isset($_POST['data1'])) {
    $opp1 = $_POST['data1'];
    $opp2 = $_POST['data2'];
} else {
    $opp1 = 'SystemID';
    $opp2 = 'ApparentPower';
}

$sql = "SELECT $opp1, $opp2 FROM RaritanMachineDataa";

在我的bargraph.html中,有两个下拉菜单。我希望使用AJAX从下拉菜单中选择的选项发送到我的data.php文件,以对数据库执行select语句。

当前,当我运行我的代码时,它正在返回错误

Uncaught ReferenceError: data1 is not defined

这指向第53行:

url: 'data.php',

有人可以帮助我,因为我不知道如何解决。

更新:(下面的代码):

<script type="text/javascript">

$('#dataform').submit(function(e){
        var data1Value = $("#data1").val();
        var data2Value = $("#data2").val();

    $.ajax({
        type: 'post',
        url: 'data.php',
        dataType: 'html',
        data: {data1, data1Value, data2: data2Value},
        success:function(data){ 
            alert(data); 
        }, 
        error:function (xhr, ajaxOptions, thrownError){
            alert(thrownError);
        }, 
        complete: function(){
        }
    });
})});
</script>

返回错误:

Uncaught SyntaxError: Unexpected token }

在线(在第二行)

})});

我尝试删除括号和大括号,但似乎无法运行它。我做错了什么?感谢您的协助

更新(“提交”按钮不发布数据):

        <option value="PowerFactor">PowerFactor</option>
    <option value="SystemID">SystemID</option>
  </select>
    <input type="submit" name="submit" value="Submit">
</form>

<script type="text/javascript">

    $('#dataform').submit(function(e){
        var data1Value = $("#data1").val();
        var data2Value = $("#data2").val();
    $.ajax({
        type: 'post',
        url: 'data.php',
        dataType: 'html',
        data: {data1, data1Value, data2: data2Value},
        success:function(data){ 
            alert(data); 
        }, 
        error:function (xhr, ajaxOptions, thrownError){
            alert(thrownError);
        }, 
        complete: function(){
        }
    });
    e.preventDefault();
});

1 个答案:

答案 0 :(得分:3)

您的错误在这里:

  $.ajax({
    type: 'post',
    url: 'data.php',
    dataType: 'html',
    data: {data1, data2: data1Value, data2Value}, <-- Your error is here

将此行更改为:

 data: {data1: data1Value, data2: data2Value}

我也刚刚注意到您正在这样做

('#data1-value').change(function(){
    var data1Value = $(this).val();
     $('#data2-value').change(function(){
        var data2Value = $(this).val();

我不相信$('#data2-value').change会被打电话。我建议您在设置了两个值之后处理表单的提交!!!

使用@Devpro注释,您的下拉菜单现在需要分配一个ID,此提交功能才能正常工作

HTML

 <select name="data1" id="data1-value"> to  <select id="data1" name="data1" id="data1-value">
 <select name="data2" id ="data2-value"> to  <select id="data2" name="data2" id ="data2-value">

JQuery

 $('#dataform').submit(function(e) {
       var data1Value = $("#data1").val();
       var data2Value = $("#data2").val();

       //ajax submit with my edits
       $.ajax....

       e.preventDefault(); //prevent page refresh.
  });