0

I'm using PHP, HTML and Javascript for my web app. Now I'm having a small issue with the following code.

$e=$_POST['users']; //$e should have either employee_id or a string "All"

Now depending on this value I've to redirect a HTML button to a specific page. In my case

if $e=="employee_id(or may be you could say that $e!="All")" then redirect the page to salary_report.php

and if $e=="All" then the page should redirect to salary_report_combined.php

My current button code is as follows :

<input type="button"  class="btn btn-primary" id="leavere" name="leavere" value="Back to Salary Report" onclick="location.replace('salary_report.php')"></input> 

So currently it is redirecting to salary_report.php file only. My issue is how should I apply a condition based on PHP variable value and execute the HTML code? Now could you help me in resolveing ithis issue. Thnks in advance.

1
  • 1
    I'd just make a link and make it look like a button and print the href with PHP based on your condition. Commented Jul 27, 2013 at 6:19

3 Answers 3

1

You can do this:

if($e == "All"){
$redirectTo = "salary_report_combined.php";
}
else{
$redirectTo = "salary_report.php";
}

In your button:

<input type="button"  class="btn btn-primary" id="leavere" name="leavere" 
value="Back to Salary Report" onclick="location.replace('<?php echo $redirectTo ?>')"></input>

But I'd just create a link with the redirect page as href, like so:

<a href="<?php echo $redirectTo; ?>"> Back to Salary Report </a>

Hope this helps!

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

Comments

1

You can also use as below:

var $e = <?php echo $_POST['users'] ?>;

then use $e in condition.

Comments

1

As a solution to your problem you can store redirection url into a php variable and then access the value of php variable within javascript code snippet.

E.g

 <?php
 $redirection_url='';   //Initialzing of variable for redirection url
 if($e=='All')
 {
     $redirection_url='salery_report_combined.php';
 }
 else
 {
      $redirection_url='salary_report.php';
 }
 ?>

    <input type="button"  class="btn btn-primary" id="leavere" name="leavere" value="Back to Salary Report" onclick="window.location.replace('<?php echo $redirection_url; ?>')"></input> 

Comments

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.