JQuery使用提交的表单数据加载div

时间:2017-03-18 18:58:18

标签: php jquery load

嗨我想从load.php页面加载一个div到我的index.php页面,提交表单数据,但我只得到文本(表单值未加载)所以这里是代码
的index.php:

<script>
    $(document).ready(function(){
        $('#result').load('load.php #load');
    });
</script>

    <div id="result" ></div>

load.php:

<div id="load">Emotions that causes your project <?php echo $test;?></div>

但它给了我:

<div id="result">Emotions that causes your project</div>

所以它不会在我的#result div上回显$ test变量,所以你可以建议我怎样才能让它工作,谢谢。

2 个答案:

答案 0 :(得分:1)

.load()发起单独的jqXHR请求(a.k.a。$ajax()电话)。

作为单独的,这个请求与你的php的应用程序逻辑的其余部分没有隐含的直接关系,并且与组成你所在页面的初始请求没有关联。

如果您需要填充$test变量,则必须在load.php脚本中定义并填充它(或包含填充它的其他.php个文件。)

将其放入load.php进行测试:

<?php $test = 'test'; ?>
<div id="load">Emotions that causes your project <?= $test;?></div>

请注意.load()允许您传递带有请求的数据,您可以在php中使用该数据生成响应。例如,将数据发送到服务器的请求:

 $('#result').load('load.php #load', {"foo":"bar"});

...并在load.php中使用该数据:

<?php $test = $_REQUEST['foo']; ?>
<div id="load">Emotions that causes your project <?= $test;?></div>

当然,您可以将foobar替换为您需要的任何内容。使用jQuery从页面中的输入元素中获取数据。

答案 1 :(得分:0)

经过大量的搜索,我解决了我的问题,所以有以下几点:
home.php:

<?php 

    $test = $_POST['test']; 

?>

<script>
    $(document).ready(function(){
        $('#result').load('load.php?test=<?php echo urlencode($test);?> #load');
    });
</script>

<div id="result" ></div>

然后在我的load.php上:

<?php 

    $test = $_GET['test']; 

?>

<div id="load">Emotions that causes your project <?= $test;?></div>

所以我在#result上加载的内容上获得了$ test值,这就是全部。

相关问题