[英]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:你有多个缺陷:
$_GET
with mysql_real_escape_string()
mysql_real_escape_string()
逃避你的$_GET
mysql_*
as those functions are deprecated.mysql_*
因为这些函数已被弃用。 Read this articleonclick
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.