使用jQuery / Ajax将id传递给PHP脚本

时间:2013-09-12 13:42:23

标签: javascript php jquery html ajax

这是用户参与的代码。

<html>
<head>
<title></title>
<script src="jquery-1.9.1.js"></script>
<script src="jquery.form.js"></script>
</head>
<body>

<?php

    include 'connection.php';

    $sql="SELECT * FROM blog";

    $result=mysqli_query($link, $sql);
        if(!$result)
            {
                $output='Error fetching students:'.mysqli_error($link);
            }
?>

<div id="table">
<table border='1' cellpadding='10' id="table">

    <tr>
        <td><b>Title<b></td>
        <td><b>Edit<b></td>
        <td><b>Delete<b></td>
    </tr>

    <?php 


        while($row=mysqli_fetch_array($result))
            {
                echo '<tr class="record">';
                echo '<td><a href="#" id="'.$row['articleid'].'" class="title">'.$row['articletitle'] .'</a>';
                echo '<td><a href="#" class="edit">Edit</a>';
                echo "<input type='hidden' name='id' value='".$row['articleid']."'></td>";  
                echo '<td><div align="center"><a href="" id="'.$row['articleid'].'" class="delbutton" title="Click To Delete">Delete</a></div></td>';
                echo "</tr>\n";


            }

    echo '<form method="post" id="myForm" action="postview.php">';
    echo '<input type="hidden" name="myID">';
    echo '</form>';

?>

</table>
<button id="addrecord">Add New Post</button>
</div>


<script type="text/javascript">
$(document).ready(function(){

$("#addrecord").click(function(){
        $("#table").load("addpost.php");
        $("#addrecord").hide();
        });//add

$(".delbutton").click(function(){

//Save the link in a variable called element
    var element = $(this);

//Find the id of the link that was clicked
    var del_id = element.attr("id");

//Built a url to send
    var info = 'id=' + del_id;
    if(confirm("Are you sure you want to delete this Record?"))
        {

            $.ajax({
                        type: "GET",
                        url: "delete.php",
                        data: info,
                        success: function(){}
                    });//ajax

            $(this).parents(".record").animate({ backgroundColor: "#fbc7c7" }, "fast")
            .animate({ opacity: "hide" }, "slow");

        }

return false;

});//delete

$(".title").click(function(){
    $('[name=myID]').val($(this).attr('id'));
    $('#myForm').submit();
});//view

$(".edit").click(function(){
    var data=$("#tryform").serialize();

            $.ajax({
                        type: "POST",
                        url: "editpost.php",    
                        data: data
                        }).done(function( msg ) {
                        $("#table").html(msg);
                    });//ajax


});//delete

});


</script>



</body>
</html>

这是上面代码重定向到的PHP脚本。

<?php 

include 'connection.php';
$id=$_GET['id'];
echo $id;
$sql="SELECT * FROM blog WHERE articleid='$id'";

$result=mysqli_query($link, $sql);
echo "<table>";

$row=mysqli_fetch_array($result);

echo "<tr>";
echo "<td>".$row['articletitle'] . "</td>";
echo "<td><img src='image.php?id=$row[articleid]' width='200' height='200' /><br/></td>";
echo "<td>".$row['articlemore'] . "</td>";
echo "</tr>";



echo "</table>";
//echo "</div>";
?>

我遇到这种错误:

未定义的索引:第4行的C:\ xampp \ htdocs \ ckeditor \ samples \ postview.php中的id

4 个答案:

答案 0 :(得分:0)

这不是错误,而是一个低级别的通知简单意味着$_GET['id']没有值

echo $example['something']; // will give undefined index notice
$example['something'] = 'abc';
echo $example['something']; // No notices

您的网站应为domain.ext/?id=123,否则,此通知会显示。

答案 1 :(得分:0)

您需要检查参数“id”是否首先传递给php脚本

<?php 

include 'connection.php';
if( (isset($_GET['id'])) && ($_GET['id'] != '') ){ //check if the argument "id" is passed to the script
    $id=$_GET['id'];
    echo $id;
    $sql="SELECT * FROM blog WHERE articleid='$id'";

    $result=mysqli_query($link, $sql);
    echo "<table>";

    $row=mysqli_fetch_array($result);

    echo "<tr>";
    echo "<td>".$row['articletitle'] . "</td>";
    echo "<td><img src='image.php?id=$row[articleid]' width='200' height='200' /><br/></td>";
    echo "<td>".$row['articlemore'] . "</td>";
    echo "</tr>";



    echo "</table>";
//echo "</div>";
}

?>

答案 2 :(得分:0)

与您的问题的答案不同,但可能是一个很好的建议,使您的代码更清洁 - 尝试使用PHP Alternative Syntax并移入和移出PHP以使您的HTML清洁。

<?php while($row=mysqli_fetch_array($result)):?>
                <tr class="record">';
                    <td><a href="#" id="<?php echo $row['articleid'];?>" class="title"><?php echo $row['articletitle'];?></a>
                    <td><a href="#" class="edit">Edit</a>
                        <input type='hidden' name='id' value='<?php echo $row['articleid'];?>'></td>
                    <td>
                        <div align="center">
                            <a href="" id="<?php echo $row['articleid'];?>" class="delbutton" title="Click To Delete">Delete</a>
                        </div>
                    </td>
                </tr>
<?php endwhile;?>

<form method="post" id="myForm" action="postview.php">
    <input type="hidden" name="myID">
</form>

答案 3 :(得分:0)

$sql="SELECT * FROM blog WHERE articleid='".$id."'";

试试这一行