从 php 获取变量到 jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18547211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:46:50  来源:igfitidea点击:

get variable to jQuery from php

phpjquery

提问by Patrick Thach

this is my variable in setings.php

这是我在 settings.php 中的变量

$error = output_errors($errors);

i want to echo out in my jQuery file 'settings.js'

我想在我的 jQuery 文件“settings.js”中回显

   $('#save_settings').click(function(){

    var first_name = $('#first_name').val();
    var last_name = $('#last_name').val();
    var email = $('#email').val();

    $.post('settings.php', { first_name: first_name, last_name: last_name, email: email}, function(data){
        $('#show').html('settings saved').fadeIn(500).delay(2000).fadeOut(500);
        alert(data);
    });


});

回答by Mainz007

If you want to communicate between JavaScript and PHP, the best way is to create a hidden-inputfield in fill in the errors-variable. And then you can read out the value with jQuery.

如果您想在 JavaScript 和 PHP 之间进行通信,最好的方法是在错误变量中创建一个隐藏输入字段。然后你可以用jQuery读出这个值。

The inputfield:

输入栏:

<input type="hidden" value="<?php echo $error; ?>" id="errors" />

And the jQuery-Code:

和 jQuery 代码:

var errors = $("input#errors").val();

回答by evilReiko

You can't place PHP code inside "javascript" file, PHP code must be in PHP file. The below code can work if it's placed in .php file:

您不能将 PHP 代码放在“javascript”文件中,PHP 代码必须在 PHP 文件中。如果将下面的代码放在 .php 文件中,它可以工作:

<html>
    <head>
        <title>javascript test</title>
        <script>
            var error = '<?php echo $error;?>';
            alert(error);
        </script>
    </head>
<body>
</body>
</html>

回答by Patrick Thach

I am assuming that your $error will by different depending on the $_POST values. If your response header is in HTML, then you can do something like this.

我假设您的 $error 会因 $_POST 值而异。如果您的响应标头是 HTML,那么您可以执行以下操作。

// in your JS codes.
$.post('settings.php', function(response) {
  $('#show').html(response).fadeIn(500).delay(2000).fadeOut(500);
});

// in your php codes.
<?php if(isset($error)) { echo($error); } else { echo("setting saved"); }  die(); ?>

回答by rove101

Anything you want in your js from server side has to come as AJAX or JSON response. echo it from your php function and get it as AJAX or JSON in javascript.

你在服务器端的 js 中想要的任何东西都必须以 AJAX 或 JSON 响应的形式出现。从您的 php 函数中回显它并在 javascript 中将其作为 AJAX 或 JSON 获取。

$.getJSON('url for the php file', function(data){}

And in the php file just echo the value

并在 php 文件中只回显值

see http://php.net/manual/en/function.json-encode.php

http://php.net/manual/en/function.json-encode.php

Or take a hidden input field and put the value there. You can get that value from js using the 'id' or 'class' attribute

或者获取一个隐藏的输入字段并将值放在那里。您可以使用 'id' 或 'class' 属性从 js 获取该值