开发者

Set Input Value of HTML Hidden TextField With PHP Using Value From jQuery

开发者 https://www.devze.com 2023-03-28 22:49 出处:网络
I have the following script: EDIT: <?php session_start(); //connect to database function connect() { $dbh = mysql_connect (\"localhost\", \"d\", \"a\") or die (\'I cannot connect to the database

I have the following script:

EDIT:

<?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 }  ?>

I have a hidden textfield with id = 'athleteI开发者_运维技巧d". I have a php array which contains values that I want to assign to the hidden textfield upon pressing the go button (i.e. in the $("#go").click event handler.

Can you do something like:

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


to change value of a textbox you do

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

of course whatever-value can be anything you want


Try to send the $ids array along with the page response as a JavaScript array.

//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>");

Now since you are not using any php variable in the above code(echos) you can directly use it as a js on your page. You can just echo the $ids array.

//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>
0

精彩评论

暂无评论...
验证码 换一张
取 消