0

I was working on populate dropdown with jquery ajax, i've got this error

Notice: Undefined index: categid in C:\xampp\htdocs\PopuTry\popucon.php on line 9

here's the code

<?php

$servername = "localhost";
$username = "root";
$password = "";
$database="maindb";


$names =$_POST['categid'];

// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
$output='';
$sql ="Select * From tbl_stocks Where Category = '".$names."'";
$result = mysqli_query($conn,$sql);
$output='<option value="">Select Menu</option>';
while($row=mysqli_fetch_array($result))
{
    $output .='<option value="'.$row["Menu_Number"].'">'.$row["Name"].'</option>';
}
echo $output;
?>

the $names code is where the error thrown,

here's my html code

    <?php
include('popucon.php');
function loadCateg()
{
    $servername = "localhost";
    $username = "root";
    $password = "";
    $database="maindb";

// Create connection
    $conn = mysqli_connect($servername, $username, $password, $database);

    $output ='';
    $sql = "select * from tbl_category";
    $result = mysqli_query($conn,$sql);
    while($row = mysqli_fetch_array($result))
    {
        $output .='<option value-"'.$row["cat_id"].'"">'.$row["Category"].'</option>';
    }
    return $output;
}
?>
<html>
<head>
    <title>Try1 Populate</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>

    <p>Select Category
    <select name="categ" id="categ">
        <option value="">Select Category</option>
        <?php echo loadCateg(); ?>
    </select></p>
    <p>Select Menu
        <select name="menu" id="menu">
            <option value="">Select Menu</option>

        </select></p>
</body>
</html>

<script>
    $(document).ready(function(){
        $('#categ').change(function(){
            var categ_id = $(this).val();
            $.ajax({
                url:"popucon.php",
                method:"POST",
                data:{categid:categ_id},
                dataType:"text"
                success: [ function (data)
                {
                    $('#menu').html(data);
                }
                ]
            });
        });
    });

</script>

The data:{categid:categ_id}, is where the categid $_POST declared.

can anyone help me? thanks!

Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
Wakaluchi
  • 5
  • 1
  • 3
  • write `data:{'categid':categ_id}` – Ravi Hirani Mar 24 '17 at 13:53
  • the error still prompting. – Wakaluchi Mar 24 '17 at 13:56
  • dataType should be json – Ravi Hirani Mar 24 '17 at 13:58
  • I thing the problem is on first load, because popucon.php in included at the top, but $_POST['categid'] is not yet defined. Change `$names =$_POST['categid'];`to `$names =isset($_POST['categid'])? $_POST['categid'] : null; ` – Jure Jager Mar 24 '17 at 13:58
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Alon Eitan Mar 24 '17 at 13:59
  • The problem Undefined index: categid in C:\xampp\htdocs\PopuTry\popucon.php on line 9 is not prompting anymore but the problem is it doesn't populate the second dropdown after user select. – Wakaluchi Mar 24 '17 at 14:02
  • @Wakaluchi what did u do? – Masivuye Cokile Mar 24 '17 at 14:05
  • the problem is solve! first i just put `$names =isset($_POST['categid'])? $_POST['categid'] : null;` then remove the `dataType` on my ajax. – Wakaluchi Mar 24 '17 at 14:08

3 Answers3

0

Try this code:

         $.ajax({
                url:"popucon.php",
                method:"POST",
                data:{categid:categ_id},
                success: function (data)
                {
                    $('#menu').html(data);
                }
            });
Star_Man
  • 1,091
  • 1
  • 13
  • 30
0

Check if it exists first, before trying to use it.

$servername = "localhost";
$username = "root";
$password = "";
$database="maindb";

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

    $names =$_POST['categid'];

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $database);
    $output='';
    $sql ="Select * From tbl_stocks Where Category = '".$names."'";
    $result = mysqli_query($conn,$sql);
    $output='<option value="">Select Menu</option>';
    while($row=mysqli_fetch_array($result))
    {
        $output .='<option value="'.$row["Menu_Number"].'">'.$row["Name"].'</option>';
    }
    echo $output;
}
Patrick Moore
  • 13,251
  • 5
  • 38
  • 63
0

Rewrite your code as below:-

 $(document).ready(function(){
    $.ajax({
        url:"popucon.php",
        method:"POST",
        data:{'categid':categ_id}, 
    })
    .done(function( data ) {
        $('#menu').html(data);
    });
});

In PHP file, check isset OR empty

if(!empty($_POST['categid'])){
    // do stuff
}
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42