如何将php发布数据传递给javascript [英] Ways to pass php post data to javascript

查看:91
本文介绍了如何将php发布数据传递给javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想知道,如果我有一个简单的PHP表单将信息发布到某个变量,我怎样才能将该发布的变量传递给javascript并能够操作数据?唯一的办法,我已经找到了通过使用内嵌的JavaScript与PHP,但我觉得必须有一个更清洁,更优雅的方式来做到这一点,变量不公开地显示在源代码中,并在外部处理..这是我现在玩的:

 < form action =index2.phpmethod =post > 
< input name =sugarclass =formtype =textvalue =/>
< input name =fatclass =formtype =textvalue =/>
< input name =proteinclass =formtype =textvalue =/>
< input type =submitvalue =submit/>
< / form>

其次是:

 <?php 
$ sugar = $ _POST ['sugar'];
$ fat = $ _POST ['fat'];
$ protein = $ _POST ['protein'];
?>

< html>
< head>
< script src =http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.jstype =text / javascript>< / script>
< script type =text / javascript>
var sugar =<?php echo $ sugar?>;
var fat =<?php echo $ fat?>;
var protein =<?php echo $ protein?>;
< / script>
< script src =test.jstype =text / javascript>< / script>
< / head>

有人对我可以遵循的替代方案有任何建议吗?我一直没有使用JavaScript太长时间,所以在处理这种情况时,我很高兴知道我的选择。

解决方案

最简单的方法就是发送一个包含你发送的所有数据的Javascript对象。这可以通过JSON编码轻松完成 $ _ POST

  var data =<?php echo json_encode($ _ POST)?>; 

然后,您就可以访问 data.fat <​​/ code>如果您在POST请求中发送了 fat <​​/ code>值。


So I was wondering, if I have a simple php form that posts information to a certain variable, how can I pass that posted variable to javascript and be able to manipulate the data? The only way I've figured out is by using inline javascript with php but I feel there must be a cleaner and more elegant way to do it where the variables don't show up so blatantly in the source code and are handled externally.. Here's what I'm playing around with right now:

<form action="index2.php" method="post">
    <input name="sugar" class="form" type="text" value="" />
    <input name="fat" class="form" type="text" value="" />
    <input name="protein" class="form" type="text" value="" />
    <input type="submit" value="submit" />
</form>

Which is followed by:

<?php 
$sugar = $_POST['sugar'];
$fat = $_POST['fat'];
$protein = $_POST['protein'];
?>

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var sugar = "<?php echo $sugar ?>";
        var fat = "<?php echo $fat ?>";
        var protein = "<?php echo $protein ?>";
    </script>
    <script src="test.js" type="text/javascript"></script>
</head>

Does anyone have any suggestions for alternatives I could follow? I haven't been using javascript for too long so it would be nice to know my options when dealing with a situation such as this.

解决方案

The easiest way to do this would be simply to send a Javascript object containing all the data you sent. This can easily be accomplished by JSON-encoding $_POST:

var data = <?php echo json_encode($_POST) ?>;

You can then access, for instance, data.fat if you sent a fat value in your POST request.

这篇关于如何将php发布数据传递给javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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