weixin_33735676 2018-05-06 20:32 采纳率: 0%
浏览 23

AJAX未执行PHP脚本

I am attempting to write an application that interacts with a library and gets values from it, and I can't seem to figure out how to get a php script to execute when a button is clicked.

Index.html

Chapter: <input type="text" name="chapter"><br>
Section: <input type="text" name="section"><br>
Problem: <input type="text" name="problem"><br>
<input type="button" class="button" name="insert" value="Get Input" 
onClick="doStuff()"/>

<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
function doStuff()
{
var v1 = document.getElementById('chapter').value;
var v2 = document.getElementById('section').value;
var v3 = document.getElementById('problem').value;

 $.ajax({
 type: "POST",
 url: "get-data.php",
 data: {chapter:v1,section:v2,problem:v3},
 success: function(data) {
      alert(data);
  }

  });
}
</script>

get-data.php

<?php

    function doit()
    {
        $chapter=$_GET['chapter'];
        $section=$_GET['section'];
        $problem=$_GET['problem'];
        $command = "python proc.py init $chapter $section $problem";
        $output = exec($command);
        echo $output; 
    }
    doit()
    ?>

the python script is confirmed to be working, and the php script is confirmed to be working when executed by using <form action="get-data.php"> but it always redirects to a simple page where the output is echo to.

  • 写回答

3条回答 默认 最新

  • weixin_33720186 2018-05-06 20:35
    关注

    You should receive values in php page by using $_POST not $_GET, because in ajax request that you wrote you are using type = "POST".

    评论

报告相同问题?