使用javascript提交表单后更改页面 [英] Change page after submitting the form using javascript

查看:63
本文介绍了使用javascript提交表单后更改页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提交表单后会转到页面 http://qwertyasdfgh.rozblog.com/New_Post(动作值)

我不想更改动作,但我想在提交后重定向到另一个页面。



我试着重定向到 http://qwerty.rozblog.com/page / b>

这里是我试过的代码:



(html)

 < form method =postaction =http://qwertyasdfgh.rozblog.com / New_Postid =f1> 
< input type =textname =titlestyle =width:300px>< br />
< input type =textname =pimgstyle =width:300pxmaxlength =3072>< br />
< textarea dir =rtlid =postname =poststyle =width:300px; aria-hidden =true>< / textarea>< br />
< input type =submitname =postsubmitvalue =submitstyle =background:#333;的onclick = 位置() >
< / form>

(js)

  function location(){
window.location.replace(http://qwerty.rozblog.com/page/success);
}

这里是小提琴

解决方案

您有两种选择。 b
$ b

1)使用AJAX提交表单并在收到服务器重定向浏览器对您所需页面的响应后。例如,您可以使用带有Ajax表单插件的jQuery。代码如下所示:

 < script src =http://ajax.googleapis.com/ajax/libs /jquery/1.7/jquery.js\"></script> 
< script src =http://malsup.github.com/jquery.form.js>< / script>

< script>
//等待DOM被加载
$(document).ready(function(){
//绑定'f1'表单并提供一个简单的回调函数
$ ('#f1')。ajaxForm(function(){
window.location =/ page / success
});
});
< / script>

OR

2)您可以离开您的表单和js原样,并使用例如php来做一些事情后重定向用户。



New_post.php

 <?php 
//一些没有打印的东西(如果您打印某些东西,您不能更改标题)

标题(位置:页/成功);


I have a form that after submitting goes to page "http://qwertyasdfgh.rozblog.com/New_Post" ( the action value )

I don't want to change the action but I want to redirect to another page after submitting.

I tried to redirect to "http://qwerty.rozblog.com/page/success" after submitting but it doesn't work .

here is the code I tried :

(html)

<form method="post" action="http://qwertyasdfgh.rozblog.com/New_Post" id="f1">
      <input type="text" name="title" style="width:300px"><br />
      <input type="text" name="pimg" style="width:300px" maxlength="3072"><br />
      <textarea dir="rtl" id="post" name="post" style="width:300px;" aria-hidden="true"></textarea><br />
      <input type="submit" name="postsubmit" value=" submit " style="background:#333;" onclick="location()">
    </form>

(js)

 function location() {
   window.location.replace("http://qwerty.rozblog.com/page/success");
}

and here is the fiddle

解决方案

You have two choices.

1) Submit that form using AJAX and after recieving response from server redirect browser to your desired page. You can use for example jQuery with Ajax form plugin. The code would look like this:

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
 <script src="http://malsup.github.com/jquery.form.js"></script> 

    <script> 
        // wait for the DOM to be loaded 
        $(document).ready(function() { 
            // bind 'f1' form and provide a simple callback function 
            $('#f1').ajaxForm(function() { 
                window.location = "/page/success" 
            }); 
        }); 
    </script>

OR

2) You can leave your form and js as is, and use for example php to redirect user after doing some stuff.

New_post.php

<?php
// some stuff without printing (you cant change headers if you print something)

Header("Location: /page/success");

这篇关于使用javascript提交表单后更改页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆