I am trying to rediect in new page if file is not present but is not redirecting,but if i only write alert then it is working. Please help me.
//php code
<?PHP
if(isset($_POST["bills"]))
{
if($_POST[month]=='Select Month' || $_POST[year]=='Select Year'){$msg="Please select valid month or year";}
else if(file_exists($path.$_POST["month"].$_POST["year"].'.pdf')){header('location:'.$path.$_POST[month].$_POST[year].'.pdf');}
else{?>
<script type='text/javascript' language="javascript">document.getElementById('downloadlink').click();</script>
<?PHP }
}
?>
//html code
<form action="electricitybills.php" method="post" >
<select name="year">
<?PHP include('include/billsyear.php'); ?>
</select>
<select name="month">
<?PHP include('include/billsmonth.php'); ?>
</select>
<div class="gobutton"><input type="submit" name="bills" value="GO"></div>
</form>
</div>
<br><br><br><a id="downloadlink" href="http://www.google.com" target="_blank" style="display:none;">link text</a></form>
click()
- Executes a click on a element as if the user manually clicked on it. In most browsers, click() only works on form INPUT elements that's non "submit" or "reset". It can't be used to simulate a click on a link or form submit button.
downloadlink
is actually a link
most probably, So that why it's not working
Refer this link for click()
documentation
Use .ready function of jquery. Because you trying to access document.getElementById('downloadlink') before its generation.
<a id="downloadlink" href="http://www.google.com" target="_blank" style="display:none;">link text</a>
<?PHP
if(isset($_POST["bills"]))
{
if($_POST[month]=='Select Month' || $_POST[year]=='Select Year'){$msg="Please select valid month or year";}
else if(file_exists($path.$_POST["month"].$_POST["year"].'.pdf')){header('location:'.$path.$_POST[month].$_POST[year].'.pdf');}
else{?>
<script type='text/javascript' language="javascript">
$('#downloadlink').trigger('click');
//html code
<form action="electricitybills.php" method="post" >
<select name="year">
<?PHP include('include/billsyear.php'); ?>
</select>
<select name="month">
<?PHP include('include/billsmonth.php'); ?>
</select>
<div class="gobutton"><input type="submit" name="bills" value="GO"></div>
</form>
</div>
<br><br><br></form>
Try this one. I hope it will help you!
In php you can use for redirection:
header('location: www.google.com');
or in javascript:
location.href = "http://www.example.com/test";