简体   繁体   English

在 javascript 中嵌入 php

[英]embedding php in javascript

hi i am new to all html javascript and php.嗨,我是所有 html javascript 和 php 的新手。 this is suppose to save the input from a html form to my database... but i have no idea why this doesnt work... the code dosent seem to go into the php section to execute the command... kindly advise thank you这是假设将输入从 html 表单保存到我的数据库...但我不知道为什么这不起作用...代码似乎进入了 php 部分来执行命令...请指教,谢谢

<html>
<script type = "text/javascript">
   function processInputs()
   {
      //get data from html form
      var email_data;
      email_data=document.getElementById('txtInput').value;
      //document.write(email_data);

      <?php
         //connect database
         $con = mysql_connect("localhost","admin","password");
         if (!$con)
           {
           die('Could not connect: ' . mysql_error());
           }

         mysql_select_db("internet", $con);
         $newemail = $_GET['email_data'];
         echo $newemail;
         //query
         mysql_query("INSERT INTO user_data (email) VALUES ($newemail)");

         mysql_close($con);
      ?>
   }
</script>

<FORM action="test.php" method="post">
    <INPUT type="text" value="this is a text" name"txtInput" id="txtInput" />
</FORM>

<button type="button" onclick=processInputs() >click!</button>

You have multiple flaws:你有多个缺陷:

  1. you cannot "embed" like that a PHP script into a javascript function, for that you should use AJAX - try jQuery .您不能像这样将 PHP 脚本“嵌入”到 javascript 函数中,为此您应该使用 AJAX - 尝试jQuery PHP is server side, Javascript is browser based. PHP 是服务器端,Javascript 是基于浏览器的。
  2. You have a MySQL injection hole there, you should escape your $_GET with mysql_real_escape_string()你有一个 MySQL 注入漏洞,你应该用mysql_real_escape_string()逃避你的$_GET
  3. Stop using mysql_* as those functions are deprecated.停止使用mysql_*因为这些函数已被弃用。 Read this article阅读这篇文章
  4. onclick is invalid, it should be <button type="button" onclick="processInputs()">click!</button> onclick无效,应该是<button type="button" onclick="processInputs()">click!</button>

If you have a perception that -如果你有这样的看法——

On calling the js function on some user actions such as button click/hover, etc would trigger the php code and populate the data,在某些用户操作(例如按钮单击/悬停等)上调用 js 函数时,将触发 php 代码并填充数据,

then YOU ARE WRONG .那么你错了

PHP is a server side scripting language, and you can't use it this way. PHP 是一种服务器端脚本语言,您不能以这种方式使用它。 The complete page is parsed at the server end and given as response to the browser.整个页面在服务器端解析并作为对浏览器的响应。 To do dynamic updates use Ajax calls that would call a php page and populate the html for you.要进行动态更新,请使用Ajax 调用,该调用会调用 php 页面并为您填充 html。

I suggest refer the coredogs.com site for better understanding of php flow.我建议参考coredogs.com站点以更好地了解 php 流程。

your function is not being called !!!你的函数没有被调用!!! Change this改变这个

onClick = processInputs() onClick = processInputs()

to

onClick = "processInputs()" onClick = "processInputs()"

and try for an alert statement to see if function is being called or not并尝试使用警报语句来查看是否正在调用函数

Also you cannot have php script within script tag你也不能在脚本标签中包含 php 脚本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM