简体   繁体   English

发布表单并重定向到PHP

[英]Post form and redirect to PHP

I'm using this jQuery code to submit a dynamic form : 我正在使用以下jQuery代码提交动态表单:

$('#formsite').on('submit', function (e) {
  //prevent the default submithandling
  e.preventDefault();
  //send the data of 'this' (the matched form) to yourURL
  $.post('inc/siteform.php', $(this).serialize());
});

but this method only sends the data to the PHP file. 但是此方法仅将数据发送到PHP文件。 I want also that it redirects me to there, as an ordinary PHP POST submission. 我还希望它将我重定向到那里,作为普通的PHP POST提交。

How can I do it? 我该怎么做?

Here is the full testing site: http://edge-americas.com/control/main.html 这是完整的测试网站: http : //edge-americas.com/control/main.html

UPDATE: 更新:

Using the method JQuery redirects me but it doesn't send the formdata at the same time so I can't use $_POST[] variables: 使用方法JQuery重定向我,但它不会同时发送formdata,因此我不能使用$ _POST []变量:

$('#formsite').on('submit', function (e) {
  //prevent the default submithandling
  e.preventDefault();
  //send the data of 'this' (the matched form) to yourURL
  $.post('inc/siteform.php', $(this).serialize(),function(response){
    window.location = "inc/siteform.php";
    });
});

Is there any other way to keep using jquery and solve it? 还有其他方法可以继续使用jquery并解决它吗?

Javascript works perfectly for this: Javascript为此完美地工作:

window.location.href = "URL";

Or as Andy pointed out if you want users to go back without issues simply drop the . 还是正如Andy指出的那样,如果您希望用户返回时没有问题,只需删除。

window.location = "URL";

You can also use window.location.replace() and pass in the URL of where you want to be redirected as a paramter. 您也可以使用window.location.replace()并将要重定向的URL作为参数传递。

Location.replace() for more information on the method. Location.replace()有关该方法的更多信息。

You can redirect or refresh page after succcess or server answer. 成功或服务器应答后,您可以重定向或刷新页面。 For example: 例如:

$.ajax({
    url:"?show=ajax_request&action=add_offer",
    type:"POST",
    data: {var_to_send : somevar},
    dataType: "json",
    success: function(answer){
        if ( answer.result == 'success' )
        {
            location.reload(); // refresh the page
        }
        else if ( answer.result == 'error' )
        {
            window.location.href = "http://google.com"; // redirect to another page
        }
   }
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM