0

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 = 'athleteId". 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]") ?>
6
  • 1
    It would be easier if you just posted the html outputted by the PHP since the PHP is not really relevant here Commented Aug 17, 2011 at 14:49
  • considering I can not concentrate and even find the commented line in a reasonable amount of time, there's got to be a better approach to doing this Commented Aug 17, 2011 at 14:50
  • Since you are only referencing ONE PHP variable in this big mess, why don't you just output all the javascript as plain output and <?php echo $the_one_variable; ?> inside it? Commented Aug 17, 2011 at 14:50
  • Not only are you mixing apples and oranges, the layout of your code is incredibly hard to read. Why don't you use heredoc syntax? And why are you thinking that you can mix php and JS in such a way? It's been covered time and time again on SO how the two CAN interact, and your way isn't right. Commented Aug 17, 2011 at 14:53
  • @Michael - isn;t the php rendered server side, therefore in the javascript, I cannot access the php - that is my problem.. Commented Aug 17, 2011 at 17:24

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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>

4 Comments

@user559142 - Did this help you? I saw your other question related to this and you have used this answer in your next question. If it helped you please mark it as an answer, thanks
Ok, changed it. You were right about the jQuery. Thanks a lot. Can you address my edited question above....how do I assign $("#atheleteId").text() to a value in the $ids php array?
I already have shown you above how to set the hidden field value.
sorry im funding it hard to follow how to implement this into my example..I have uploaded my code..are you sure I can do it your way with the way I ahve written it so far? Or will I have to change much?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.