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.
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:
Back to Salary Report
Hope this helps!
You can also use as below:
var $e = <?php echo $_POST['users'] ?>;
then use $e in condition.
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>
Related
I am not able to reload the php page after a certain button is pressed. That is there is no change until I reload the page by myself. The php script is:
<?php
// Initialize the session
session_start();
if (($_SESSION['ShowGenerated'] == 'hidden'))
{
$class = 'hidden';
}
else if (($_SESSION['ShowGenerated'] == 'visible'))
{
$class = 'show';
}
?>
<html>
<head>
....
<td><button id="pdf" name="Change" class="btn btn-primary"><i class="change-a"" aria-hidden="true"></i>Click Here</button></td>
...
On clicking "Click here" button, Change.php is called where $_SESSION['ShowGenerated'] value is generated. How do I load the current page or a URL to show a change?
EDIT: Instead of reloading the whole page, can I only reload to check if the $_Session has changed in the html page?
Try to put tag inside :
<button id="pdf" name="Change" class="btn btn-primary"><i class="change-a"" aria-hidden="true"></i>Click Here</button>
like this
I have been having problems passing Javascript variable to php using ajax on the same page.I have a try.php page and once a button is clicked, I want a value sent to try.php variable without reloading the page..
This is my form code,I am actually looping trough a database record
<?php
foreach($vars as $var){
?>
<form method="POST">
<button class="btn btn-warning btn-sm btn_edit" name="btn_edit" value="<?php echo $var['mem_id'];?>">
<span class="glyphicon glyphicon-pencil"></span></button>
<?php
}
?>
Here is my ajax code
$('.btn_edit').click(function(e){
var uid = $(this).val()
$.post("try.php",{ btn_edit : uid},
function(){
});
console.log(uid);//I could see the id logged on console
$('#edit_details').modal('show');
e.preventDefault();//prevent the form from submitting
});
On my try.php page, I have this code to check if it passed succesfully
<?php if(isset($_POST['btn_edit'])){
$user_id = $_POST['btn_edit'];
}?>
There are two modifications need to be done:
Change 1: Only input elements can have value attribute. And as you have <button>, it should be assigned with some other attribute which can be captured using jQuery, so here I am going to use data-bind attribute with it.
<button class="btn btn-warning btn-sm btn_edit" name="btn_edit" data-bind="<?php echo $var['mem_id'];?>">
And then,Change 2:
Get data-bind value with jQuery like this:
$('.btn_edit').click(function(e){
var uid = $(this).attr('data-bind'); // Using attr, not val()...
$.post("try.php",{ btn_edit : uid},
function(){
});
console.log(uid);//I could see the id logged on console
$('#edit_details').modal('show');
e.preventDefault();//prevent the form from submitting
});
im trying to show the user's name $user on a button but i cant figure how to do it:
here's some of the code
include ("config.php");
include ("adminchk.php"); // Check if user is admin
include ("getdir.php");
include ("lang/lang_".$lang.".php");
if (isset($uStat) and $uStat===TRUE)
{
echo $GLOBALS['l_menuWelcome'] . " " . $user."\n";
if ($admin===TRUE) echo "*";
echo "<div class='quote'>\n"; ... etc
and here's the button code :
<button type="button" class="btn btn-default" onclick="location.href='userconnect.html'" >$user</button>
thanks for your time :)
To print a PHP variable in the middle of some HTML, you need to wrap it in PHP tags and use echo. So assuming your button code comes from a PHP file, the code looks like this:
<button type="button" class="btn btn-default" onclick="location.href='userconnect.html'" >
<?php echo $user; ?>
</button>
Documentation
I have the following:
$allowed_referer = array("https://localhost/**blah.php**"); //add the allowed sites in this array
$referal = $_SERVER['HTTP_REFERER'];
if (in_array($referal, $allowed_referer)){
//Do-stuff
}
so if the page is coming from blah.php the save button is allowed to be displayed instead of submit.
I have it set up where the save button updates the database and the submit button inserts into the database.
<?php if (!in_array($referal, $allowed_referer)){ ?>
<button type="submit" name="submit" value="submit-new"> Submit </button>
<?php }
else { ?>
<button type="submit" name="submit" value="save-new"> Save </button>
<?php } ?>
How can I make sure the save button stays even after clicking on save.
for example, lets say the page is coming from blah.php, then you make some changes and click the save button, then it refreshes the page which makes it believe it is not coming from "$allowed_referer" array, so it will then change the save button to the "submit" button, which is bad because now instead of saving that current id, submit will insert a duplicate of the file you just wanted to save.
You could store the information user in comming from a known referer in the first place when you ckeck it the first time:
$allowed_referer = array("https://localhost/**blah.php**"); //add the allowed sites in this array
$referal = $_SERVER['HTTP_REFERER'];
if (in_array($referal, $allowed_referer)){
//Store a flag in the session
$_SESSION['come_from_known_referer'] = true;
//Do-stuff
}
And then modify your code:
<?php if (!isset($_SESSION['come_from_known_referer'])){ ?>
<button type="submit" name="submit" value="submit-new"> Submit </button>
<?php } else { ?>
<button type="submit" name="submit" value="save-new"> Save </button>
<?php } ?>
As a sidenote, remember that a user can hide or modify the headers send to your server so do not rely on this for critical functionalities.
This question already has answers here:
How to submit a form to two different pages depending on the button clicked, without javascript
(5 answers)
Closed 9 years ago.
I have a form where I can add a new category item.
<form method="POST" action="backend/categories/form">
<input type="text" name="title" value="" />
<button type="submit">Save</button>
<button type="submit">Save and add new</button>
</form>
What I want to do, is that if i click on Save button, it will process a function in controller and redirect me automatically into the previous page (list of categories page), but whenever I click on Save and add new, it should process the function but reload the same page without redirecting to the page which is defined in controller's function.
Controller:
function form($id){
// Process the form
// ...
// Redirect to the category list page
redirect($this->config->item('backend_folder').'/categories');
}
Any tips to achieve it without using the Javascript ?
Use this HTML:
<form method="POST" action="backend/categories/form">
<input type="text" name="title" value="" />
<button type="submit" name="submitForm" value="formSave">SAVE</button>
<button type="submit" name="submitForm" value="formSaveNew">SAVE AND ADD NEW</button>
</form>
Then check the POST data like this:
$formSubmit = $this->input->post('submitForm');
if( $formSubmit == 'formSaveNew' )
redirect($this->config->item('backend_folder').'/categories/form');
else
redirect($this->config->item('backend_folder').'/categories');
Disclaimer: I didn't try that.
This might be helpful for you:
Use this in your save function in your controller. Immediate after Insert/Update code:
$task = $_POST['submit'];
//echo $task //Save or SaveNew depending on pressed button
switch ($task)
{
case 'Save':
$this->session->set_flashdata('message',$this->lang->line('changes_has_been_saved_successfully'));
$link = redirect('cashdrawer/cashdrawerform/12'); //Link after save button with id
break;
case 'SaveNew':
$this->session->set_flashdata('message',$this->lang->line('this_order_has_been_saved_successfully'));
$link = redirect('cashdrawer/cashdrawerform'); //Link after save and new
//echo $link;
}
redirect($link);
And Change your button as:
<button type="submit">Save</button>
<button type="submit">SaveNew</button>