我很难用Ajax脚本获取PHP $ _GET变量,目标是使用相同的脚本更新MySql表。这是我用于获取和显示数据的PHP / HTML脚本:
session_start();
require_once ('dbconnect.php');
$query = mysql_query("SELECT p_id, p_name, p_authors, p_corresponding, p_email, p_cauthor, p_abstract, p_keywords, p_jname, p_date FROM papers INNER JOIN users ON papers.user_id = users.user_id WHERE p_url = '$val' AND username='{$_SESSION['user']}'");
$last = mysql_num_rows($query);
$output1 = "";
$outarray1 = array();
if ($last > 0) {
while ($output1 = mysql_fetch_array($query)) {
$outarray1[] = $output1;
if(is_array($outarray1)){
foreach($outarray1 as $values){
$id = $values['p_id'];
$paper = $values['p_name'];
$author = $values['p_authors'];
$corresponding = $values['p_corresponding'];
$mail = $values['p_email'];
$coauthors = $values['p_cauthor'];
$abstract = $values['p_abstract'];
$keywords = $values['p_keywords'];
$journal = $values['p_jname'];
$date = $values['p_date'];
}
}
}
}
echo <<<HERE
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="memberModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="memberModalLabel">Edit Paper Details</h4>
</div>
</div>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Paper</th>
<th>Author</th>
<th>Corresponding author</th>
<th>Mail</th>
<th>Co-authors</th>
<th>Abstract</th>
<th>Keywords</th>
<th>Journal</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<tr>
<td>{$id}</td>
<td>{$paper}</td>
<td>{$author}</td>
<td>{$corresponding}</td>
<td>{$mail}</td>
<td>{$coauthors}</td>
<td>{$abstract}</td>
<td>{$keywords}</td>
<td>{$journal}</td>
<td>{$date}</td>
<td><a class="btn btn-small btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="<?php echo $id; ?>">Edit</a></td>
</tr>
</tbody>
</table>
这是同一页面上的AJAX / JQuery脚本,用于使用$ _GET方法从表中获取ID变量,但问题是,当单击编辑按钮时,$ _GET变量不会显示在URL中,也不会在editfile中被选中页面,我只能看到&#34;编辑纸张详细信息&#34;标题,下面的空格是空的,它应该显示用于编辑记录的字段:
<script>
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var recipient = button.data('whatever') // Extract info from data-* attributes
var modal = $(this);
var dataString = 'id=' + recipient;
$.ajax({
type: "GET",
url: "editdata.php",
data: dataString,
cache: false,
success: function (data) {
console.log(data);
modal.find('.ct').html(data);
},
error: function(err) {
console.log(err);
}
});
})
</script>
这是editfile脚本:
<?php
session_start();
require_once ('dbconnect.php');
?>
<html lang="en">
<head>
<link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<form method="post" action="editdata.php" role="form">
<div class="modal-body">
<div class="form-group">
<label for="name">ID
<input type="text" id="id" name="id" value="<?php echo $id;?>" readonly="true"/>
</label>
</div>
<div class="form-group">
<label for="name">Paper
<input type="text" id="name" name="paper" value="<?php echo $paper;?>" />
</label>
</div>
<div class="form-group">
<label>Author
<input type="text" id="job" name="author" value="<?php echo $author;?>" />
</label>
</div>
<div class="form-group">
<label>Corresponding author
<input type="text" id="service" name="corr" value="<?php echo $corresponding;?>" />
</label>
</div>
<div class="form-group">
<label>Email
<input type="text" id="education" name="mail" value="<?php echo $mail;?>" />
</label>
</div>
<div class="form-group">
<label>Co-authors
<input type="text" id="education" name="cauthors" value="<?php echo $coauthors;?>" />
</label>
</div>
<div class="form-group">
<label>Abstract
<input type="text" id="education" name="abstract" value="<?php echo $abstract;?>" />
</label>
</div>
<div class="form-group">
<label>Keywords
<input type="text" id="education" name="keywords" value="<?php echo $keywords;?>" />
</label>
</div>
<div class="form-group">
<label>Journal
<input type="text" id="education" name="journal" value="<?php echo $journal;?>" />
</label>
</div>
<div class="form-group">
<label>Date
<input type="text" id="education" name="date" value="<?php echo $date;?>" />
</label>
</div>
</div>
<input type="submit" class="btn btn-primary" name="submit" value="Update" />
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</form>
<?php
$id = $_GET['id'];
if (isset($_POST['submit'])) {
$paper = $_POST['paper'];
$author = $_POST['author'];
$corresponding = $_POST['corr'];
$mail = $_POST['mail'];
$coauthors = $_POST['cauthors'];
$abstract = $_POST['abstract'];
$keywords = $_POST['keywords'];
$journal = $_POST['journal'];
$date = $_POST['date'];
$qry = "UPDATE papers INNER JOIN users ON papers.user_id = users.user_id SET p_name = '$paper', p_authors = '$author', p_corresponding = '$corresponding', p_email = '$email', p_cauthor = '$coauthors', p_abstract = '$abstract', p_keywords = '$keywords', p_jname = '$journal', p_date = '$date' WHERE p_id = '$id' AND username = '{$_SESSION['user']}'";
mysql_query($qry) or die("Error !".mysql_error());
header("Location: home.php");
}
?>
有什么建议吗?
答案 0 :(得分:0)
我假设你的&#34;编辑文件脚本&#34;是来自你的ajax的editdata.php
。
1)我看到你试图通过GET传递ID的位置,但是你从哪里获得所有发布的数据?
2)使用GET时,您不必再指定它的GET数据,只需将其附加到URL ...
url: "editdata.php?"+dataString,