【问题标题】:Save value of select after sending page via php with javascript使用 javascript 通过 php 发送页面后保存选择的值
【发布时间】:2017-08-09 23:10:13
【问题描述】:

大家晚上好,我有一个问题,我正在寻找可以帮助我的人。

我有以下问题,我生成了一个 select,其中所有选项都来自数据库,然后我使用 php 通过 post 操作发送信息。

我希望在返回页面时,使用选择选项,在最后选择的值对表单处于活动状态之后。是否可以通过javascript或其他方式做到这一点?

*抱歉英语不好

    $sql = "SELECT * FROM curso WHERE data  = '$date'";
    $result = mysql_query($sql, $conexao2);

    echo "<select name=\"curso\" >";

    while($linha = mysql_fetch_row($result)) {
        $nome_evento = $linha[1];
        $cod_evento = $linha[0];
        echo  "<option  value=".$cod_evento.">".$nome_evento."</option>"."<br>" ;
    } 
    echo  "</select>";

【问题讨论】:

  • 你好。您是否尝试将选择框设置为上一个选定项目。
  • 您需要做的是获取选定的值并将其作为变量传递,然后您可以执行 php if 在循环期间进行检查,然后将 selected 值设置为与当前搜索的匹配,请参阅下面的说明
  • mysql_* 函数已弃用,您需要使用 mysqli 或 PDO。
  • Enstage - 我知道这已经过时了,感谢您指出。我需要学习 PDO。

标签: javascript php jquery


【解决方案1】:

好的,您还需要获取之前输入的值。

 $sql = "SELECT * FROM curso WHERE data  = '$date'";
 $result = mysql_query($sql, $conexao2);

 $currentlySelectedValue = ///Add your code here to get this value

 echo "<select name=\"curso\" >";

 while($linha = mysql_fetch_row($result)) {
    $nome_evento = $linha[1];
    $cod_evento = $linha[0];
    echo  '<option value=' . $cod_evento . ' ' . (($cod_evento === $currentlySelectedValue) ? 'selected' : '') . '>' . $nome_evento . '</option>';
 }
 echo  "</select>";

那么option 区域发生的事情是:

(($cod_evento === $currentlySelectedValue) ? 'selected' : '')

即检查所选值与您输出的值,如果匹配,则会将selected 值添加到其中。

希望这对您有所帮助。

【讨论】:

  • 我现在要去测试了。非常感谢。
  • 我不会在这里待几个小时,但希望你有一个开始,如果附近的其他人可以提供帮助,谢谢
  • 谢谢,工作正常,感谢您浪费时间 kkk。
  • 太棒了,只需将其标记为正确答案,很乐意提供帮助。
  • PS:正如@Enstage 提到的,请查找PDO。不要使用过时和不安全的 mysql_query php.net/manual/en/book.pdo.php w3schools.com/php/php_mysql_prepared_statements.asp 它更安全和最新
【解决方案2】:

@Anderson Augusto 是否可以通过 javascript 或其他方式做到这一点?

是的,您可以通过 Ajax 访问它而无需重新加载页面

1) 创建 ajax.php 在里面你有这个

<?php 
class Curso {

    // Print select option value as soon as page load
       public function curso($date){

$sql = "SELECT * FROM curso WHERE data  = '$date'";
    $result = mysqli_query($sql, $conexao2);
     while($linha = mysqli_fetch_row($result)) {
        $nome_evento = $linha[1];
        $cod_evento = $linha[0];
        return ("<option  value=".$cod_evento.">".$nome_evento."</option>");
    } 


         }
}

?>

2) 创建 frontpage.php 在里面你有这个

   <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>My front page</title>
    </head>

    <body>
    <form id="CursoForm">
    <?php 
    require("ajax.php");
    $OptionValues= new Curso();
    $TheValues =$OptionValues->curso("YOUR-DATE");
    ?>  
    <!--Maybe some other inputs-->
    <input type="text">
    <select name="curso"><?php echo($TheValues);?></select>
    </form>

    <script>
//==================================
$("form#CursoForm").on("submit",function(event) {
event.preventDefault();
$.ajax({
type: "POST",
url: "path/to/your/further/function.php",
cache: false,   
dataType: 'html',//choose what you are expecting e.g. JSON | HTML | TEXT
data: $(this).serialize(),
success: function(data){      
//handle do something with re
},
error: function(){      
 //handle do something wth the error 
}
});
});
//  
</script>
    </body>
    </html>

【讨论】:

  • 感谢您抽出宝贵时间,但我已经解决了问题,非常感谢。
猜你喜欢
  • 1970-01-01
  • 2016-12-22
  • 2014-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多