2015-09-08 56 views
0

我有困難的時間獲取帶有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">&times;</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腳本從表中讀取ID變量,$ _ GET方法,但問題是,當編輯按鈕點擊時,$ _ GET變量沒有URL顯示,也沒有在EDITFILE頁面被拾起,我只能看到「編輯文件細節」稱號,下方空間是空的,它應該顯示爲編輯記錄字段:

<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> 

這是編輯文件腳本:

<?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" />&nbsp; 
<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

只是一個警告:你正在使用PHP的'的mysql_query() '和相關功能。請注意,此API已棄用(並且將在下一個PHP版本中完全刪除)。您應該考慮將代碼切換爲使用PDO或mysqli庫。 – Simba

回答

0

我假設你的「編輯文件腳本」是你的ajax中的editdata.php

1)我看到你試圖通過GET傳遞ID的位置,但是你從哪裏獲取所有後期數據?

2)當使用GET你不relly需要指定它的獲取數據,只是其附加到網址...

url: "editdata.php?"+dataString, 
+0

是的,我有那個,忘了在這裏c/p – Greedy

+0

@Greedy - 這就是爲什麼它支付張貼你的實際代碼,或者如果你要改變它,確保它發佈之前按預期工作。 –

+0

抱歉,我修改了它 – Greedy