【问题标题】:Set Input Value of HTML Hidden TextField With PHP Using Value From jQuery使用来自 jQuery 的值使用 PHP 设置 HTML 隐藏文本字段的输入值
【发布时间】:2011-08-17 14:46:49
【问题描述】:

我有以下脚本:

编辑:

<?php
session_start();
//connect to database
function connect() {
  $dbh = mysql_connect ("localhost", "d", "a") or die ('I cannot connect to the database because: ' . mysql_error());
  mysql_select_db("PDS", $dbh); 
  return $dbh;
}

if(isset($_SESSION['username'])){
  $dbh = connect();
  $id = $_SESSION['id'];
  $sql="SELECT a.athleteId, a.fName, a.lName FROM Athletes a, SupportStaff s, StaffAthletes sa WHERE sa.staffId = $id AND a.athleteId = sa.athleteId"; 
  $result=mysql_query($sql); 
  $ids = array();
  $options="";   
  $i = 1;
  while ($row=mysql_fetch_array($result)) { 
    $ids[]=$row['atheleteId'];
    $f=$row["fName"]; 
    $l=$row["lName"]; 
    $options.="<OPTION VALUE=\"$i\">".$f.' '.$l."</OPTION>"; 
    $i++;
  }

  $array = "";
  for($x = 0; $x < count($ids); $x++)
    {
      $array .= "[" . $ids[$x][0] . ", ". $ids[$x][1] . "]" ; 
      if($x+1 < count($ids))
        $ids .= ", ";
    }
  $idsArray = "new Array( " . $array . ")";

  echo("<script>");
  echo("var $ids = ".$idsArray);
  echo("</script>");
?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   <head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
   <script src = "jQuery.js"></script>
   <script>
   $(document).ready(function(){
       $("#go").click(function(e){
       if($("#selectathlete option:selected").val() == "0"){
         alert("Please Select An Athlete");
       }else{
         //set hidden textfield to id of selected athlete   
         //NEED TO ACCESS $ids ARRAY HERE
         $("form#athleteselect").submit();
       }
       });
   });
  </script>
  <title>Personal Diary System - Home Page</title>
  </head>
  <body>
  <h1>Home Page</h1>
  <p>Select An Athlete: 
  <SELECT ID ="selectathlete" NAME="athletes"> 
  <OPTION VALUE="0">Choose</OPTION>
  <?php echo($options);?>
  </SELECT>
  </p>
  <form id = "athleteselect" name="athleteselect" action="viewathleteprofile.php" method="post">
  <input type = "hidden" name = "hidden" id = "athleteId" value = "">
  <input type = "button" name = "go" id = "go" value = "Go"/>
  </form>
  </body>
</html>
<?php }  ?>

我有一个 id = 'athleteId" 的隐藏文本字段。我有一个 php 数组,其中包含我想在按下开始按钮时分配给隐藏文本字段的值(即在 $("#go").click 事件处理程序中。

你能做这样的事情吗:

$("#athleteId").text() = <?php echo("$ids[4]") ?>

【问题讨论】:

  • 如果你只是发布 PHP 输出的 html 会更容易,因为 PHP 在这里并不真正相关
  • 考虑到我无法集中注意力,甚至无法在合理的时间内找到注释行,因此必须有更好的方法来做到这一点
  • 既然你在这个大杂烩中只引用了一个 PHP 变量,你为什么不把所有的 javascript 输出为纯输出并在里面输出&lt;?php echo $the_one_variable; ?&gt;
  • 您不仅将苹果和橙子混为一谈,而且您的代码布局也非常难以阅读。你为什么不使用heredoc语法?你为什么认为你可以以这种方式混合 php 和 JS?关于两者如何交互的内容已被一次又一次地覆盖,而您的方式不正确。
  • @Michael - 不是 php 呈现的服务器端,因此在 javascript 中,我无法访问 php - 这是我的问题..

标签: php javascript jquery html option


【解决方案1】:

更改文本框的值

$("#id-of-your-textbox").attr("value","whatever-value");

当然,whatever-value 可以是你想要的任何值

【讨论】:

    【解决方案2】:

    尝试将$ids 数组与页面响应一起作为JavaScript 数组发送。

    //This code will render the php variable as a js array along with page response
        $array = "";
        for($x = 0; $x < count($ids); $x++)
        {
        $array .= "[" . $ids[$x][0] . ", ". $ids[$x][1] . "]" ; 
        if($x+1 < count($ids))
        $ids .= ", ";
        }
        $idsArray = "new Array( " . $array . ")";
    
      echo("<script>");
      echo("var $ids = ".$idsArray);
      echo("$(document).ready(function(){");
      echo("$(\"#go\").click(function(e){");
      echo("var selText = $(\"#selectathlete option:selected\").text();");
      echo("if($(\"#selectathlete\").val() == \"0\"){");
      echo("alert(\"Please Select An Athlete\");");
      echo("}else{");
      echo("$(\"#athleteId\").val($ids[$(\"#selectathlete\").val()]);"); //not working
      echo("$(\"form#profile\").submit();");
      echo("}");
      echo("});");
      echo("});");
      echo("</script>");
    

    现在,由于您在上述代码(回声)中没有使用任何 php 变量,因此您可以直接将其用作页面上的 js。你可以直接回显$ids 数组。

    //This code will render the php variable as a js array along with page response
            $array = "";
            for($x = 0; $x < count($ids); $x++)
            {
            $array .= "[" . $ids[$x][0] . ", ". $ids[$x][1] . "]" ; 
            if($x+1 < count($ids))
            $ids .= ", ";
            }
            $idsArray = "new Array( " . $array . ")";
    
    echo("<script>");
    echo("var $ids = ".$idsArray);
    echo("</script>");
    
    
    <script type="text/javascript">
        $(document).ready(function(){
            $("#go").click(function(e){
                var selText = $("#selectathlete option:selected").text();
                if($("#selectathlete").val() == "0"){
                    alert("Please Select An Athlete");
                }else{
                    $("#athleteId").val($ids[$("#selectathlete").val()]);
                    //If you want the fourth index as per your edited question use this
                    //$("#athleteId").val($ids[4]);
                    $("form#profile").submit();
                }
          });
        });
    </script>
    

    【讨论】:

    • @user559142 - 这对您有帮助吗?我看到了与此相关的其他问题,并且您在下一个问题中使用了此答案。如果对您有帮助,请将其标记为答案,谢谢
    • 好的,改了。您对 jQuery 的看法是正确的。非常感谢。你能解决我上面编辑的问题....如何将 $("#atheleteId").text() 分配给 $ids php 数组中的值?
    • 我已经在上面展示了如何设置隐藏字段值。
    • 对不起,我很难理解如何在我的示例中实现这一点..我已经上传了我的代码..你确定我可以按照我迄今为止编写的方式来做吗?还是我必须改变很多?
    猜你喜欢
    • 2013-04-20
    • 2011-06-15
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多