简体   繁体   English

使用ajax将表单发布到php而不刷新

[英]Using ajax to post a form to php without refresh

I have this written up for send a few variables to a php script: 我已经写了一些用于向php脚本发送一些变量的代码:

$(function() {

$('a[class="removeUnread"]').click(function(){

   var markedtopicid = $(this).attr("id");
   var sessionvar     = \'', $context['session_var'], '\';
   var sessionid = \'', $context['session_id'], '\';


   $.ajax({
      url: "index.php?action=quickmod;board=', $context['current_board'], '", 
      type: "POST",
      data: sessionvar + "=" + sessionid + "&topics[]=" + markedtopicid + "&qaction=markread",
    });

});

}); });

I think this is the correct way to send post data via ajax, but it doesn't appear to be sending. 我认为这是通过ajax发送帖子数据的正确方法,但似乎没有发送出去。 Was I right to wrap the code in the ready function? 我将代码包装在ready函数中对吗?

I can tell you right off the bat you should not have a semicolon between quickmod and board in your URL. 我可以立即告诉您,您的URL中不应在quickmod和board之间使用分号。 I'm answering here because i cannot post comments yet. 我在这里回答,因为我还不能发表评论。 One good tool to use in web development ESPECIALLY with GET and POST requests is googles PostMan app. 谷歌PostMan应用程序是一种特别适用于GET和POST请求的Web开发工具。 Its free to use and what it does is it will show you the exact output of any link you send it. 它免费使用,它的作用是向您显示您发送的任何链接的确切输出。 So you can try putting the link that you make via javascript into postman and see what errors it spits out. 因此,您可以尝试将通过javascript制作的链接放入邮递员,看看它吐出了什么错误。

In this example i'm pretty sure your URL is all kinds of screwed up though. 在此示例中,我非常确定您的URL各种各样。 Try this instead... 试试这个...

"index.php?action=quickmod&?board="+$context['current_board']

fyi, i did not test that link so it may not work. 仅供参考,我没有测试该链接,所以它可能无法正常工作。 If it doesnt work, google some ajax examples and javascript string concatenation. 如果不起作用,请使用Google的一些Ajax示例和javascript字符串连接。 You're string is not suitable for ajax. 您的字符串不适合Ajax。

is should be like this... 应该是这样的...

$.ajax({
                url :'index.php',
                type : 'POST',
                data : { sessionvar: sessionid, topics:markedtopicid}, 
                success : function (data) {

                        },
                error : function () {

                        }

Try handling the error: 尝试处理错误:

$.ajax({
      url: "index.php?action=quickmod;board=', $context['current_board'], '", 
      type: "POST",
      data: sessionvar + "=" + sessionid + "&topics[]=" + markedtopicid + "&qaction=markread",
    error: function (xhr, ajaxOptions, thrownError) {                
         alert(xhr.responseText);
     }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP表单(带有刷新)到AJAX(不带有刷新) - PHP Form (with refresh) to AJAX (without refresh) 使用php和ajax上传文件,而无需使用通常的表单发布方法 - Upload file with php and ajax without using the usual form post method 使用AJAX POST到PHP并在更改时刷新 - POST to PHP using AJAX and refresh upon change 如何通过以下方式提交表单:PHP发送电子邮件,使用Ajax进行屏幕刷新,然后在不进行屏幕刷新的情况下在表单旁边发布消息? - How do I submit form via: PHP sending email, Using Ajax for no screen refresh, and post a message next to form with no screen refresh? ajax on使用php提交无刷新表单 - ajax on submit no refresh form using php 带有ajax的表单-没有刷新页面的消息表单php - form with ajax - message form php without refresh page 使用ajax提交表单将表单传递给php而不刷新页面 - Form submit with ajax passing form to php without page refresh 使用PHP AJAX显示成功消息而不显示页面刷新后,再次显示Form div - Show Form div again after displaying success message without Page Refresh Using PHP AJAX 如何在不刷新页面和 URL 路径的情况下使用 ajax 和 php 发送表单数据 - How to send form data using ajax and php without page refresh and URL path AJAX jquery移动php表单提交不刷新不更新 - AJAX jquery mobile php form submission not updating without refresh
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM