duanjucong3124 2017-08-10 13:11 采纳率: 100%
浏览 52

使用AJAX将javascript变量传递给PHP无法正常工作[重复]

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;}
</div>
  • 写回答

1条回答 默认 最新

  • dtyz76562 2017-08-10 14:00
    关注

    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 ?

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化