0

im trying to pass a js variable(a value that i get when user selects a random option in select dropdown menu) into a php variable so i can check the attributes of the selected option in my database.. Option is the name of the AD and value is actually its ID in the database. When user selects an option method getSelected() gets called. In this method i first try to pass the ID value stored in js ( value = e.options[e.selectedIndex].value; ) to a PHP variable using AJAX. The file i am writing in is actually "uredi.php". So I am wondering what I am doing wrong here. I am open to any suggestions. I create a select box with php which works fine. The problem is the error:

Undefined index: suggestion in C:\xampp\htdocs\mod\sub\uredi.php on line 38

here is the code:

<body>
    <div class="generalBox">
        <form class="generalForm" action="" method="POST">
            <label class="headLabel">UREDI</label>
            <br>
            <br>
            <script>
            var value;
            var opis;
            var datumz;
            var datumk;
            var pr;
            var format;
                document.write(<?php echo json_encode(getCurrentAds()); ?>);

                function getSelected(){

                    var e = document.getElementById("ogSelect");
                    value = e.options[e.selectedIndex].value;
                    console.log(value);

                    $.post("uredi.php", {
                        suggestion: value
                    }, function(data, status){
                        $("#test").html(data);
                    });

                    <?php $id = $_POST['suggestion']; ?>
                    }
                }
            </script>
            <br>
            <br>
            <input class="inputTextSub" id="opisE" name="opisE" type="text">
            <br>
            <br>
            <input class="inputTextSub" id="datumzE" name="datumzE" type="date">
            <br>
            <br>
            <input class="inputTextSub" id="datumkE" name="datumkE" type="date">
            <br>
            <br>
            <label class="inputTextSub">prioriteta</label>
            <select class="inputSelSub" id="prE">
                <option value=1>1</option>
                <option value=2>2</option></select>
            <br>
            <br>
            <label class="inputTextSub">format</label>
            <select class="inputSelSub" id="formatE">
                <option value=1>A3</option>
                <option value=2>A4</option></select>
            <br>
            <br>
            <input type="file" name="fileE" id="fileE" class="inputfile" />
            <br>
            <br>
            <p id="test" class="inputTextSub"></label>
            <input class="button" style="margin-top:2em;" name="saveE" type="submit" value=" shrani ">
        </from>
    </div>
</body>

here is the getCurrentAds() if you are wondering...

function getCurrentAds(){
$dropDown='';
$query = 'SELECT `oglas_id`,`naziv` FROM `deska` WHERE `mod_id`='.$_SESSION['mod_id'].';';
$result = mysqli_query($GLOBALS['conn'],$query);
$rows = mysqli_num_rows($result);
if($rows > 0){
    $dropDown = '<select onclick="getSelected()" class="inputSelAds" id="ogSelect" name="ogSelect">';
    while($row = mysqli_fetch_assoc($result)){
            $dropDown = $dropDown.'<option value="'.$row['oglas_id'].'">'.$row['naziv'].'</option>';
    }
    $dropDown = $dropDown.'</select>';
}else{
    $dropDown = '<select><option value=""></option></select>';
}
return $dropDown;}
  • @JonStirling I know there is a lot of this stuff on here but nothing helped me so far. I wouldnt post this unless I were really stuck – Gašper Božič Aug 10 '17 at 13:14

1 Answers1

0

I am confused about your line <?php $id = $_POST['suggestion']; ?>. This is supposed to put in $id the POST variable suggestion, which is not defined here (you can check it with if(isset($_POST['suggestion'])){ /* ... */ }). However no input has name="suggestion" so even after you submit the form you will get an undefined index error. What input value do you want to get in $id ?

Salicorne
  • 36
  • 4