dongrong9053 2014-05-14 23:33
浏览 39
已采纳

为什么ajax什么都不做?

So I'm trying to get PHP to send the promo code back through ajax; why is it not receiving it?

I have tried a couple different methods but none seem to be working

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<script type="text/javascript">

function submit() {
 $.ajax({
     url: 'index.php',
     type: POST,
     data: $('form.ajax').serialize(),
     success: function(response) {
         alert(response);
     }
 });
}

</script>

HTML:

  <select name="app">
    <option value="balloon">Balloon Doop</option>
    <option value="option2">Option 2</option>
    <option value="option3">Option 3</option>
  </select>


<input type="submit" id="submit1" value="submit"  onclick="submit()" data-icon="info" />

PHP Code

<?php

$nothing = "";
$option = "balloon";
if ($_POST['app'] == $option) {

$promo = file('balloon.txt');
echo ($promo[0]);
$data = file_get_contents("balloon.txt");
$newdata = str_replace($promo[0], $nothing, $data);
file_put_contents("balloon.txt", $newdata);
} else {
    echo ("Not available");
}

?>
  • 写回答

1条回答 默认 最新

  • douguachi0056 2014-05-14 23:37
    关注

    You did not cancel the default submit action of the form so the page refreshes.

    function submit(e) {
        e.preventDefault();
        $.ajax({
            url: 'index.php',
            type: POST,
            data: $('form.ajax').serialize(),
            success: function(response) {
                alert(response);
            }
        });
    }
    
    //needs to be called on document ready
    $('form.ajax').on("submit", submit);
    

    If you insist on doing it the way you have it coded.

    Add return false; at the function and onclick="return submit()"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作