简体   繁体   English

Ajax Jquery将变量传递到PHP文件

[英]Ajax Jquery Passing variable to PHP file

Hey Im having an issue in accessing the variable from JQuery Ajax. 嘿,我在从JQuery Ajax访问变量时遇到问题。 I ve tried everything. 我已经尝试了一切。 I even added cdn script tag to both files. 我什至在两个文件中都添加了cdn脚本标签。 but I keep getting this error of undefined index 但我不断收到未定义索引的错误

Notice: Undefined index: head in C:\\xampp\\htdocs\\Project\\View.php on line 20 注意:未定义索引:在第20行的C:\\ xampp \\ htdocs \\ Project \\ View.php中

Anyone has any idea wats wrong with the syntax. 任何人都有语法错误的想法。 I have attached both my files below. 我已经在下面附加了我的两个文件。

SearchProjects.php SearchProjects.php

<html>
<head>
    <meta charset="windows-1252">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/bootstrap.min.css">
   <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
     <script>

    $(document).ready(function() {
        $('#assign tr').click(function() {
            var href = $(this).find("a").attr("href");
            if(href) {
                window.location = href;
            }
        });
    });

    $('td').on('click',function() { 
       var heading=$(this).text();
        alert(heading); 
        $.ajax({
            url: "View.php",
            type: "POST",
            data: {head: heading},
            success: function(data){
                alert("success");
            }
        });    
    });

</script>

</head>
<body>

    <div> 
        <div class="col-md-12"><br/></div>
            <?php
            session_start();

            $No="Not Assigned";
            require("DB.php");
            $query="SELECT `ID`, `Assigned_By`, `name`, `path`, `heading`,`Assigned_to`, `Completed`, `Date`, `Due_Date`, `Price` FROM `assign` where `Assigned_to`='Not Assigned' order by Date Desc";

            $result=mysqli_query($PDB,$query);

            if ($result->num_rows > 0) {

                echo "<table class=table table-hover id=assign>"
                ."<thead>"
                        . "<tr> "

                    . "<th>ID</th>"
                    . "<th>Assigned_By</th>"
                    . "<th>Name</th>"
                    . "<th>Path</th>"
                    . "<th>Heading</th>"
                    . "<th>Assigned_To</th>"
                    . "<th>Completed</th>"
                    . "<th>Due_Date</th>"
                    . "<th>Submission_Date</th>"
                    . "<th>Price</th>"
                    . "</tr> </thead> ";


                while($row = $result->fetch_assoc()) {

                    echo "<tr>"
                    . "<td>".$row["ID"]."</td>"
                    . "<td>".$row["Assigned_By"]."</td>"
                    . "<td>".$row['name']."</td>"
                    . "<td>".$row['path']."</td>"
                    . "<td>".$row['heading']."</td>"
                    . "<td>".$row['Assigned_to']."</td>"
                    . "<td>".$row['Completed']."</td>"
                    . "<td>".$row['Date']."</td>"
                    . "<td>".$row['Due_Date']."</td>"
                    . "<td>".$row['Price']."</td>"
                    . "<td><a class=btn btn-default href=View.php role=button>Show More!</a></td>"
                    . "</tr>";
            }
            echo "</table>";
            } 
            else {
                echo "0 results";
            }

    ?>

    </div>
</body>

View.php View.php

<html>
<head>
    <title>TODO supply a title</title>
    <meta charset="windows-1252">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/bootstrap.min.css">

</head>
<body>
    <?php

    session_start();

    if(isset($_POST['head'])){

        echo $_POST['head'];
    }

    $filename="CV.docx";
   // $filename=$_SESSION['filename'];
    //echo $filename."<br/>";

    require("DB.php");

    $query="SELECT `heading` FROM `assign` where `name`='$filename'";

    $result=mysqli_query($PDB,$query);

    if ($result->num_rows > 0) {

        while($row = $result->fetch_assoc()) {
            $heading=$row['heading'];
            $_SESSION['heading']=$heading;
        } 
    } 
    else {
        echo "0 results";
    }
    $dir = "Assigns/";

        if (is_dir($dir)){
            if ($dh = opendir($dir)){
                while (($file = readdir($dh)) !== false){
                    if(!($file=="." || $file=="..")){     
                        $f=explode(".",$file);

                        if(strcmp($f[0],$heading)){


                            if(!file_exists("Assigns/$file")) {
                                die("File not found");
                            }   
                            else{

                                $my_file = "Assigns/$file";
                                $handle = fopen($my_file, 'r');
                                $data = fread($handle,filesize($my_file));

                            }

                        }
                    }
                }
                closedir($dh);
            }
        }
    if($_SERVER['REQUEST_METHOD']== 'POST'){

        if (isset($_POST['save'])) {

            $assigned_to=$_SESSION['username'];

            echo $assigned_to;
            echo $filename;
            $query="UPDATE `assign` SET `Assigned_to`='$assigned_to' WHERE `name`='$filename'";

            $result=mysqli_query($PDB,$query);

            if($result){
                echo "wohooo";
            }
            else{

            echo "nooo";
            }
        }
        else if (isset($_POST['submit'])) {
        //    echo "submit";
            header('location: Solution.php');

        }

    }

    ?>
    <div class="container">
        <form action="View.php" method="post">
            <h1 style="clear:both;"><?php echo $heading."<br/>" ?> </h1> 

            <div class="form-group">
                <?php echo $data; ?>
            </div>

        <div class="col-md-12 col-md-offset-6">
            <input type="submit" name="submit" value="Submit Solution">
            <input type="submit" name="save" value="Save Changes">
        </div>

       </form>
    </div>
</body>

Always check the existence of a variable/array index before using it: 使用它之前,请始终检查变量/数组索引是否存在:

$head = isset($_POST['head']) ? $_POST['head'] : null;
//may check nullity of $head

There seem to be two things going on: First of all, the click handler for the td is not attached because the DOM was not ready, wrap your jQuery code in 似乎发生了两件事:首先,由于DOM未准备好,所以未附加td的单击处理程序,将jQuery代码包装在

$(function() {
     //your code
})

Also, the click event on the tr and on the td are both fired... 此外, trtd上的click事件都被触发...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM