![](/img/trans.png)
[英]how to use ajax to put value from javascript variable into a php variable
[英]How to use PHP variable value in javascript?
我的Web應用程序使用的是PHP,HTML和Javascript。 現在,以下代碼有一個小問題。
$e=$_POST['users']; //$e should have either employee_id or a string "All"
現在,根據此值,我必須將HTML按鈕重定向到特定頁面。 就我而言
if $e=="employee_id(or may be you could say that $e!="All")" then redirect the page to salary_report.php
if $e=="All" then the page should redirect to salary_report_combined.php
我當前的按鈕代碼如下:
<input type="button" class="btn btn-primary" id="leavere" name="leavere" value="Back to Salary Report" onclick="location.replace('salary_report.php')"></input>
因此,當前它僅重定向到Salary_report.php文件。 我的問題是我應該如何基於PHP變量值應用條件並執行HTML代碼? 現在,您可以幫助我解決ithis問題。 提前感謝。
你可以這樣做:
if($e == "All"){
$redirectTo = "salary_report_combined.php";
}
else{
$redirectTo = "salary_report.php";
}
在您的按鈕中:
<input type="button" class="btn btn-primary" id="leavere" name="leavere"
value="Back to Salary Report" onclick="location.replace('<?php echo $redirectTo ?>')"></input>
但是我只需要創建一個帶有重定向頁面的鏈接,如href
,就像這樣:
<a href="<?php echo $redirectTo; ?>"> Back to Salary Report </a>
希望這可以幫助!
您還可以如下使用:
var $e = <?php echo $_POST['users'] ?>;
然后使用$ e作為條件。
為了解決您的問題,您可以將重定向網址存儲到php變量中,然后在javascript代碼段內訪問php變量的值。
例如
<?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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.