使用POST将变量从js传递到php [英] pass variable from js to php using POST

查看:485
本文介绍了使用POST将变量从js传递到php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能将变量从js传递给php

Whay I can't pass a variable from js to php

 $("#btnpage").click(function(){
        path = $('#spantwrap').html();
        $.ajax({
            url: 'plus-page.php',
            type: 'post',
            data: {'path': path},
            success: function() {
                console.log(path);
            }
        });
    location.href = 'plus-page.php';
    });

plus-page.php

<form id="form1" action="?" method="post">    
<input type="hidden" name="path" value="<?php echo $_POST['path'];?>" // line 46
</form>

错误:未定义的索引:第46行上的路径...

Error: Undefined index: path on line 46...

推荐答案

为什么不将表单发布方法与该解决方案一起使用? ?

Why don't you use form post method with this solution ??? ?

第一个Ajax成功完成后将其发布:函数称为...在新窗口中您没有任何发布数据,我不知道任何解决方案都可以通过javascript将数据发布到新窗口..只有一个您可以发送数据通过GET请求..

First Ajax post it completed when success: function called... In new window you doesn't have any post data, I don't know any solution post data by javascript to new window.. only one you can send data by GET request ..

您可以看到ajax发布的变量,并在新窗口中打开url.请使用

You can see variable posted by ajax and open url by new window.. please use

无需使用ajax即可实现所需的功能.

With out using ajax ,to achieve what you required.

$(document).on('click', '#button', function(){
    var my_data = 'Test Test Tes';
        window.open('plus-page.php?my_data='+my_data);
  });

plus-page.php

plus-page.php

<?php 
 if(isset($_GET['my_data'])){
        $data=$_GET['my_data'];    
        echo $data;          
 }
?>

这篇关于使用POST将变量从js传递到php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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