-5

I am using the following code to change button when it is clicked by echoing the javascript's document.getElemenById property but the button remains as it is:

while($result=mysql_fetch_array($result1))
                    {
                        $i=1;
                        echo "
                        <tr>
                        <form action='student.php' method='post'>
                        <td>$result[0]<input type=\"hidden\" name=\"company\" value=\"$result[0]\"></td>
                        <td>
                        <input type=\"checkbox\" name=\"yes\" />
                        <input type=\"submit\" name=\"apply$i\" id=\"apply$i\" class=\"buttons\" style=\"border: none\" value=\"Apply now!\" 
                                  onclick=\"return confirm('Are You Sure you want to apply?');\">   
                        <input type=\"hidden\" name=\"apply$i\" value=\"apply$i\">
                        </td>
                        </form>
                        </tr>
                        <tr><td><input type=\"text\" id=\"ankur\"></td></tr>
                        ";
                        $i++;
                    }
                    echo "</table></center>";

Here the button with the id apply$i should change to Applied string as per the following code

$id=$_SESSION['sessionid'];
if(isset($_POST['apply1']))
{
    if(isset($_POST['yes']))
    {
        $yes=trim(mysql_prep($_POST['yes']));
        $company=trim(mysql_prep($_POST['company']));
        $button_id=trim(mysql_prep($_POST['apply1']));
        if($yes=="on")
        {
            $query ="select `Branch`,`Class` from `student_details` where `Userid`='$id'"; 
            $temp1= mysql_query($query);
            $temp2= mysql_fetch_array($temp1);
            $query="insert into `student_applied` values ('$id' ,'$temp2[0]' ,'$temp2[1]' ,'$company')";
            //$query="UPDATE `student`.`users` SET  `Activated` =  '1' WHERE  `users`.`Userid` =  '$student'";
            $result = mysql_query($query);
            confirm_query($result);
            if($result)
            {
            echo "ok";
            echo "<script language='javascript' type='text/javascript'>document.getElementById('apply1').innerHTML ='Applied';</script>"

Does anybody know the reason?Plz help

Shadowpat
  • 3,577
  • 2
  • 14
  • 14
Ankur
  • 269
  • 2
  • 4
  • 15
  • 1
    I'd guess that you have multiple items with id set to 'apply1'. Why are you setting $i to 1 inside the loop each time? – Tony May 02 '13 at 18:16

1 Answers1

1

Instead of settingg $i inside loop, you should set it outside and then increment value, like

$i=1;
while($result=mysql_fetch_array($result1)){
    //Your rest of code to output content
    $i++;
}

That way you could have different ID for each button, and Javascript should work perfectly for you.

sven
  • 775
  • 5
  • 14
  • But still its not working i ama wondering whether javascript code should run as echo statement in PHP or not? – Ankur May 02 '13 at 18:40
  • 1
    Ya that's fine, you can echo the javascript code, it'll be echoed as it is. Can you provide me a link or detailed code, to check it?? – sven May 02 '13 at 18:41
  • 1
    As mentioned above... your loop is wrong. The way it is built now, every field is getting apply1 as an ID. You should be able to see that happening if you just view the source after the page is rendered. – Mattt May 02 '13 at 18:43
  • ya i know that its "apply1" but i was just testing it for the first value but the echo "document.get..." ; doesn't seem to work – Ankur May 02 '13 at 18:46
  • Umesh the code works as after submitting the button the button should change to a string with the help of "document.getElementBYID" but its not working.plz help – Ankur May 02 '13 at 18:49
  • whole code is in same file or there are two files? – sven May 02 '13 at 18:56
  • The thing you are asking for might have total different approach then the one you are using, If I didn't got your requirement wrong. You want to display suppose ten rows, submit button for each row, and then click on submit button you want to display a message "Applied" for that particular row. If that's the case you gotta use Ajax for that. – sven May 02 '13 at 19:02