Ajax没有提交到URL

时间:2014-04-05 18:23:43

标签: javascript php jquery ajax

当我在同一页面上调用POST函数时,它正在提交它回显我提交的内容,但是当我尝试在页面上回显它时,提交给它的内容不会出现。

除了调用完整网址的“include”函数外,其他所有内容都一样。

Page:

<script>
$(function () {
$('form#PTan').on('submit', function(e) {
$.ajax({
type: 'POST',
url: '/submittest.php',
success: function (o) {
console.log(o)
alert('MUST ALERT TO DETERMINE SUCCESS PAGE');
}
});
e.preventDefault();
});
});     
</script>

<form action='' id='PTan'  method='post'>
<select class='formstyle' id='PTan' name='PTan'>
  <option value='Beige'>Beige</option>
  <option value='Dark'>Dark</option>
  <option value='Light'>Light</option>
</select>
   <input type='submit' name='submit'  value='Submit' />
</form>

<?php
   $PTan = $_POST['PTan'];

   echo $PTan;

   include "/submittest.php";

?>

Submittest.php

<?php
$PTan = $_POST['PTan'];

echo $PTan;
?>

2 个答案:

答案 0 :(得分:0)

你错过了jquery图书馆吗?如果是,请将其添加到<head></head>

<script>
$(function () {
$('form#PTan').on('submit', function (e) {
    e.preventDefault();
    $.ajax({
        type: 'POST',
        url: '/submittest.php',
        data: $('#PTan').serialze(), //Add data attr to the ajax function 
        success: function (o) {
            console.log(o);
            alert('MUST ALERT TO DETERMINE SUCCESS PAGE');
        }
    });

});

});
     

<form action='' id='PTan'  method='post'>
<select class='formstyle' id='P' name='PTan'>
  <option value='Beige'>Beige</option>
  <option value='Dark'>Dark</option>
  <option value='Light'>Light</option>
</select>
   <input type='submit' name='submit'  value='Submit' />
</form>

答案 1 :(得分:0)

您的服务器端代码正在尝试检查$_POST['PTan'],但您尚未在Ajax请求中发送该代码。

$.ajax({
  type: 'POST',
  data: { 
      "PTan": "something" 
  },
  url: '/submittest.php',