duannian4784 2012-12-04 06:48
浏览 46
已采纳

通过Javascript将PHP注入Value属性

I am trying to inject a value stored in a PHP $_SESSION variable into the "value" attribute of an input tag using javascript. I am able to successfully inject the PHP code in the "value" attribute, but the stored value does not appear in the text box. My code is below:

HTML:

<input type="text" name="fname" value="" />

Javascript:

fields.eq(n).attr('value', "<?php echo $_SESSION['fname'] ; ?>");

The problem is that this would usually place the value of the $_SESSION['fname'] variable in the input box. But that is not the case. When the button is clicked to inject the PHP code into the value attribute, this is what the HTML looks like:

<input type="text" name="fname" value="<?php echo $_SESSION['fname'] ; ?>" />

And this is what I want it to look like but in the text box, it actually shows the PHP code and not the value stored in $_SESSION['fname'].

Is there something I am missing? Shouldn't this work? Any help is appreciated!

enter image description here

  • 写回答

3条回答 默认 最新

  • dsceme82487 2012-12-04 07:25
    关注

    PHP is a server-side script, which means: PHP will be executed on the server.

    JavaScript is a client script, which means: JavaScript will be executed by the browser.

    Usually by default, .php files will be processed by the PHP engine on the server, thus executing the PHP scripts. The result will be sent to the client. That's why when you have a .php file, things between <?php and ?> will be processed by the PHP engine and you will not see the actual PHP script in the client. Sometimes you can configure the HTTP server such that .html files are also processed by the PHP engine.

    For other files, the HTTP server (e.g. Apache) will directly sent the whole file to the client, without being processed by the PHP engine, so that any PHP scripts will not be processed and will directly go to the client.

    Now your problem is that you tried to put some PHP into a JavaScript file (.js), and hoping that it will be executed, but in fact it will not. So when you try to set the value of your input element with your shown JavaScript, the "text" <?php echo $_SESSION['fname'] ; ?> will be set.

    If you either put the JavaScript into your .php file, or rename your JavaScript file (*.js) to *.php, you will get the value you desired.

    Or probably you could add an extra JavaScript in your page, setting a "global" variable with your PHP, and use that "global variable" in your .js file.
    i.e. window.myFname = <?php echo $_SESSION['fname'] ; ?>;


    Depending on what you want to do, you may need to choose how to design. If you just simply want to set the value, you may just simply do one of the two suggestions I've provided. If you want to get the value dynamically (i.e. obtain the value when you need it), you would like AJAX like others have suggested.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计