javascript - ajax php javascript 集成不起作用

标签 javascript php html ajax

我有一个 html 页面,我通过 ajax 将数据发送到 PHP,但代码不起作用。 HTML

<form name='tip' method='post' action=''>
        Tip somebody: <input name="tip_email" id=tip_email type="text" size="30"  />
        <input type="submit"   value="Skicka Tips" />
        <input type="hidden" id="ad_id" name="ad_id" />
</form>
<script>
        $('#submit').click(function() {
                event.preventDefault();
                $.ajax({
                        url: insertdata.php,
                        type: 'POST',
                        data: {
                                tip_email: $('#tip_email'),
                                ad_id: $('#ad_id')
                        },
                        success: function(res) {
                                if (res == 'successful') {
                                        alert("successful");
                                } else {
                                        alert("failed");
                                }
                        },
                        error: function() {
                                $('#status').html('Failed').slideDown();
                        }
                });
        });
</script>

PHP

<?php
echo "successful"
?>

我在 stackoverflow 上尝试了各种可用选项,但没有一个有效。

enter image description here

Uncaught ReferenceError: $ is not defined

$('#submit').click(function() 

仍然无法工作 但是,当我将上面的脚本包含到代码中时,它会进入无限循环并抛出错误“未捕获的范围错误:超出最大调用堆栈大小” 我对现有代码做了三处更改。 1)添加脚本src 我也尝试过添加其他类似的脚本,但都显示相同的错误 这意味着代码本身存在一些问题。 2)在语句中添加事件 $('#submit').click(函数(事件) 3)将php放在单引号中 url: 'insertdata.php'。

最佳答案

请更正代码

1.) add script to below your form close - <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

2.) add id in submit button - <input type="submit" id="submit"  value="Skicka Tips" />

关于javascript - ajax php javascript 集成不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44734981/

相关文章:

html - 有什么工具可以告诉我使用了哪些 CSS 类来设置特定元素的样式?

javascript - 如何在不使用 JS 的情况下滚动到 TOC 页面上的固定位置元素之外?

javascript - 排毒 - 在基本的 iOS RN 应用程序上努力达到 "build the Detox version of the binary, and run the tests"

javascript - 使用 window.getSelection() 检查文本是否突出显示

javascript - Angular 2 : how to update service variable in a module's component so that the variable in lazy module updates automatically?

javascript - 如何在 View 之间来回维护代码点火器php中的文件读取指针?

html - 为什么没有 data-* 元素?

javascript - 如何使用javascript获取元素的高度,包括填充和边框

php - preg_replace 替换动态图片 URL

php - 来自文件的反射类 PHP?