开发者社区> 问答> 正文

js在不跳转的情况下向一个页面传递参数怎么实现?

需求是把textarea传递到savexml.php进行数据保存,但同时我希望页面还留着当前页面。
代码如下:

 <form name="form1" id="form1" method="post" action="newtest.php">
            <input type="hidden" name="text1" value="<?php echo $text1;?>" size="30"/><br>
            <textarea name="textarea1" rows="10" cols="150"><?php echo $text1;?></textarea><br>
            <input type="button" name="finish" value="完成拆分"  onClick="save()"   />
        </form>

 function save(){
               //省略的代码是获得textarea的值
                var formdeal= document.getElementById("form1");
                 formdeal.action="savexml.php?parms="+str;
               formdeal.target="_self";
                formdeal.submit();}

后台接收如下:

       $str1=$_REQUEST['parms'];?>

展开
收起
小旋风柴进 2016-03-06 16:00:44 2922 0
1 条回答
写回答
取消 提交回答
  • <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.2.min.js"></script>
     <form name="form1" id="form1" method="post" action="newtest.php">
                <input type="hidden" name="text1" value="<?php echo $text1;?>" size="30"/><br>
                <textarea name="textarea1" rows="10" cols="150"><?php echo $text1;?></textarea><br>
                <input type="button" name="finish" value="完成拆分"  onClick="save()"   />
            </form>
    <script>
        function save() {
            $.ajax({ url: 'savexml.php',type:'POST', data: $('#form1').serialize(), complete: function (xhr) {
                if (200 == xhr.status) alert('请求成功!')
                else alert('动态页有问题!\n' + xhr.responseText)
            } 
            });
        }</script>

    后台接收如下:

     <?php
           $text1=$_POST['text1'];
           $textarea1=$_POST['textarea1'];?>
    2019-07-17 18:54:48
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
JavaScript面向对象的程序设计 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载