Javascript pdo调用来填充变量以供以后使用
尝试根据单击事件从数据库获取信息,并将数据加载到变量中,以便稍后在表单中使用。这是代码的一部分。已使用PDO:建立到reps表的连接:Javascript pdo调用来填充变量以供以后使用,javascript,php,mysql,svg,pdo,Javascript,Php,Mysql,Svg,Pdo,尝试根据单击事件从数据库获取信息,并将数据加载到变量中,以便稍后在表单中使用。这是代码的一部分。已使用PDO:建立到reps表的连接: function fill_contact(evt) //display click event { var country_id = evt.target.id document.getElementById('country_name').firstChild.data = country_id &l
function fill_contact(evt) //display click event
{
var country_id = evt.target.id
document.getElementById('country_name').firstChild.data = country_id
<?php
$country = $_GET['country_name'];
try {
// Establish server connection and select database
$dbh = new PDO("mysql:host=$host;dbname=$database", $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
die("Sorry, we could not connect you to this database, please contact the Administrator to sort this out.". $e->getMessage());/** try open contact form here**/
}
/** run prepare to select records **/
$stmt = $dbh->prepare("SELECT firstname, lastname, email, photo FROM reps WHERE country = :country");
$stmt->bindParam(':country', $country, PDO::PARAM_STR);
$stmt->execute();
?>
<script language="javascript" type="text/javascript">
function fill_contact(evt) //display click event
{
var country_id = evt.target.id
document.getElementById('country_name').firstChild.data = country_id
/* need to get a ajax http fetch statement here???*/
}
function getDataFile(mapString , useData)
{
/** call getURL() if available **/
if (window.getURL) {
getURL(mapString , useData);
}
else
/** call XMLHttpRequest() if available **/
if (window.XMLHttpRequest)
{
function XMLHttpRequestCallback()
{
if (xmlRequest.readyState == 4) { useData({success:xmlRequest.status,content:xmlRequest.responseText,contentType:xmlRequest.getResponseHeader("Content-Type")})
}
}
var xmlRequest = null;
xmlRequest = new XMLHttpRequest();
xmlRequest.open("GET",mapString,true);
xmlRequest.onreadystatechange = XMLHttpRequestCallback;
xmlRequest.send(null);
}
/** write an error message if either method is not available **/
else {
alert("your browser/svg viewer neither supports window.getURL nor window.XMLHttpRequest!");
}
}
</script>
<body>
<div id="IndexPage_Left_top">
<h1>Country: <span class="label" id="country_name"> </span> </h1>
<table >
<tbody>
<tr>
<td><h4>Regional Representative: </h4></td>
<td><h4><span class="label" id="firstname">$firtname</span> <span class="label" id="lastname">$lastname</span></h4></td><!--get Rep name from database based on country from svg to display-->
</tr>
<tr></tr>
<tr height="300px" align="top">
<td ><h4> Photo:</h4></td><!--get Rep photo from database based on country from svg to display-->
<td><span class="photo" id="photo"></td>
</tr>
<tr>
<td></td>
<td><input style='margin-top: 20px;' type="submit" value="Contact me" id='jqxButton' /></td>
</tr><!-- if clicked, it opens a contact form below, the email address is hidden-->
</tbody>
</table>
</div>
<div id="IndexPage_Left_bottom">
<h4>email form here<h4>
<p> email address is <span class="label" id="email">$email</span> </p><!-- if email is clicked, it opens a contact form here with email address from above -->
</div>
</div>
<div id="IndexPage_Right">
<svg id="map" version="1.1" rest of svg code
这是我关于svg的另一个问题。单击svg后,它将填充用于显示所单击国家数据的变量。这些变量还用于填充联系人表单
它不想填充表?PHP代码中有错误 更改代码$query=$dbh->prepare'SELECT firstname,lastname,reps发来的电子邮件,其中'country_id'=:country' 到
所以问题在于这些行,它们应该是这样的:
$query = $dbh->prepare('SELECT firstname, lastname, email FROM reps WHERE country = :country_id');
$query->bindValue(':country_id', $country, PDO::PARAM_INT);
一方面,参数名为country_id,但您将该值绑定到参数country。PDO很棒,但它还不能做心灵感应
我更喜欢使用位置参数,消除所有这些容易出错的手写:
$query = $dbh->prepare('SELECT firstname, lastname, email FROM reps WHERE country = ?');
$query->execute(array($country));
您始终可以向execute方法传递一个数组,其中包含要绑定的值。对于位置参数,数组必须是0索引数组,其中包含与查询顺序匹配的值。您也可以使用命名参数执行此操作,而无需匹配顺序:
$query->execute(array(':country_id'=>$country));
要知道,你永远不能把两者混为一谈。如果您使用命名参数准备查询,则它们必须都是命名参数,并按此绑定;如果您使用位置占位符,则它们必须都是位置占位符,并按此绑定。您是否测试了web服务以获得预期的正确输出。您好。即使您认为这是一个简单的语法错误,也应该在这里复制/粘贴确切的错误。还有,你提到了你的另一个问题?还有什么问题?你能给它添加一个链接吗?按你说的更改了代码。仍然得到语法错误。我可能有点慢,已经编程3个月了。婴儿步plse。谢谢,这帮了大忙!我的荣幸。另外,我刚刚注意到您仍然在使用mysql。一、 我建议你读书。事实上,如果问题中的代码运行正常,我会非常惊讶,因为似乎有一些语法错误。如果我能提出另一个建议来帮助你学习,那么养成习惯,添加ini_集合“显示错误”,1;错误报告全部;在每个php脚本的顶部。这可能真的对你有帮助。我利用了你的建议,读了很多书。我终于让它工作起来了:现在的问题是编写并学习如何使用XMLHttpRequest,以及如何在click事件中使用数据库中的数据。我的代码现在是
$query->execute(array(':country_id'=>$country));