-2

I am able to get javascript variable output into php variable,but i am not able to store php variable value stored from javascript variable into database its storing the whole script for eg:

<script>document.write(answer)</script>

I tried on update to redirect on registration page but it remain static on the same page.

Here is my code :

if(isset($_GET['status'])) 
{
if($_GET['status']=="Rejected")
{
echo("<script type='text/javascript'> var answer = prompt('Please enter'); </script>");?>
<script>var a = document.write(answer);
window.location.href = window.location.href+'#reason='+answer;</script>
<?php
if(isset($_GET['reason']))
{
$reason=$_GET['reason'];
$approve="UPDATE approveleave SET status='Rejected',rejectreason='".$reason."' WHERE id='".$id."'";
$result2=mysql_query($approve,$con);
header("location:registration.php");
}
}
}   
?>

Any solution will be appreciated.

Thanks

Vimal
  • 1,140
  • 1
  • 12
  • 26
Vinit Sinkar
  • 99
  • 2
  • 10

3 Answers3

1
 <?php
if(isset($_GET['status']) && $_GET['status']=="Rejected") 
{
?>
<script type='text/javascript'> var answer = prompt('Please enter');
</script>
<script>var a = document.write(answer);
    window.location.href = window.location.href+'&reason='+answer;
</script>
<?php
    if(isset($_GET['reason']))
    {
        $reason=$_GET['reason'];
        $approve="UPDATE approveleave SET status='Rejected',rejectreason='".$reason."' WHERE id='".$id."'";
        $result2=mysql_query($approve,$con) or die(mysql_error());
        echo "<script> window.location = 'registration.php';</script>";
    }
}   
?>

I have reformatted your code and rather than using <a> tag, you should use query ? to pass variables and read with $_GET[] method.

We can not see if you have database connection done or not anywhere. So, I have also added mysql_error(), if there is any problem with mysql connection, you should see error.

Amit
  • 451
  • 1
  • 6
  • 19
0

To pass the data throught url you should use "?" character

your script

 window.location.href = window.location.href+'#reason='+answer;</script>

how it should be

window.location.href = window.location.href+'?reason='+answer;</script>

Reformating your code:

if(!empty($_GET['status']) && $_GET['status'] == "Rejected") {
        if(!empty($_GET['reason'])) {
            $reason = $_GET['reason'];
            $approve="UPDATE approveleave SET status='Rejected',rejectreason='".$reason."' WHERE id='".$id."'";
            $result2=mysql_query($approve,$con);
            header("location: /registration.php");
        } else {
          ?>
          <script type='text/javascript'> 
            var answer = prompt('Please enter'); 
            var a = document.write(answer);
            window.location.href = window.location.href+'?reason='+answer;
          </script>
          <?php
        }
}

Use empty() function to check if a array is empty or not.

Note: The isset() is used to check if a variable is defined or not.

The best way to interact with database is PDO. Don't use mysql_query() because it is deprecated in the newer versions of Php. You can use mysqli instead.

Neo Morina
  • 391
  • 2
  • 13
0

You code may be like this

<?php 
if(isset($_GET['status'])) 
{
     if($_GET['status']=="Rejected")
    {

            echo("<script type='text/javascript'> var answer = prompt('Please enter'); </script>");?>
        <script>var a = document.write(answer);
        window.location.href = window.location.href+'&reason='+answer;</script>

<?php if(isset($_GET['reason'])) {
                      $reason=$_GET['reason'];
                        $approve="UPDATE approveleave SET status='Rejected',rejectreason='".$reason."' WHERE id='".$id."'";
                        $result2=mysql_query($approve,$con);
                      header("location:registration.php");
                  }


    }
}   
?>
Suman Biswas
  • 853
  • 10
  • 19