0

Hey Im having an issue in accessing the variable from JQuery Ajax. I ve tried everything. I even added cdn script tag to both files. but I keep getting this error of undefined index

Notice: Undefined index: head in C:\xampp\htdocs\Project\View.php on line 20

Anyone has any idea wats wrong with the syntax. I have attached both my files below.

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

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

8
  • 2
    can you add the view.php script too? Commented Jan 8, 2015 at 13:34
  • 4
    Look at your browser's developer tools. Look at the Net tab. Is the request being made? Does it contain the data you expect? Commented Jan 8, 2015 at 13:36
  • Which browser are you using? Commented Jan 8, 2015 at 13:48
  • Im using Google Chrome Commented Jan 8, 2015 at 13:53
  • 1
    Ctrl+Shift+J opens your dev tools. Go to Network tab and make the Ajax Request. Than click on this request, select Header and you can see which post values are sent to the PHP file. Commented Jan 8, 2015 at 13:58

2 Answers 2

1

Always check the existence of a variable/array index before using it:

$head = isset($_POST['head']) ? $_POST['head'] : null;
//may check nullity of $head
Sign up to request clarification or add additional context in comments.

5 Comments

I did that too still Im getting this undeifned index error.
Here you go . View.php <?php if(isset($_POST['head'])){ echo $_POST['head']; } ?>
Show us some real code. The error is on line 20. Hide sensitive data.
Okay U can check it now
Now its not printing anythin I guess the $_POST['head'] is still empty
0

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

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

Also, the click event on the tr and on the td are both fired...

5 Comments

Still not working Is there any alternative way for passing a JS variable to PHP file ??
I created a JSFiddle: jsfiddle.net/w62y184y . If you look in the network tab, you see a POST request with the correct form data (of course in the fiddle, the request returns a 404).
Mine is still stuck at GET!!
How do I change it to POST?
In my JSFiddle? The AJAX call is correctly set to POST, so the GET you are seeing should be something else. Probably the view-more-link. Did you (temporarily) remove the tr event handler? Or change the view-more-link to something different so you can distinguish the two.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.