How to add value of $user variable to button name? - php

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

Related

How can I remove all items from the cart in PHP?

I already have some items in the cart. I want to remove them all. So, I've created a submit button with post method called 'remove_all', following doesn't work, it says "The requested URL was not found on this server." Can please someone help me?
<?php
if (isset($_POST['remove_all'])){
unset($_SESSION['cart']);
echo "<script>window.location = 'cart.php'</script>";
}
?>
And here what I wrote in my index.php
<form action="remove_all" method="post" class="cart-items">
<button type="submit" value="Очистить всё" name="remove_all" class="btn btn-danger mx-2">Очистить всё</button>
</form>
It looks like you you not linking properly to your remove_all.php file. Try this:
<form action="/remove_all.php" method="post" class="cart-items">
<button type="submit" value="Очистить всё" name="remove_all" class="btn btn-danger mx-2">Очистить всё</button>
</form>
Also in your PHP file, you do not need to mix PHP and js - simply change header as following:
<?php
if (isset($_POST['remove_all'])){
unset($_SESSION['cart']);
// echo "<script>window.location = 'cart.php'</script>";
header("Location: cart.php");
}
?>

Not able to reload a page after clicking on a button in php

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

Deleting from a table does not remove row

DISCLAIMER: I am aware of SQL Injection, I am getting the function done first.
My delete from button does not seem to remove my row in my SQL table
products-one-time.php
This is the output of my foreach, the numbers below the delete button are the groupids, just to show they are unique.
this is the code for the delete button, it redirects to clinics_buttons.vc.php on click.
<a href="clinics_buttons.vc.php<?php echo '?delete-coupongroup='.$rowCouponGroup['groupid']; ?>" onclick="return confirm('Delete this group?');">
<button class="btn btn-danger btn-sm full_width" data-toggle="modal">
<i class="fa fa-edit"></i>DELETE
</button>
</a>
<?php echo $rowCouponGroup['groupid']; ?>
clinics_buttons.vc.php
<?php
session_start();
$routePath = "../";
require_once($routePath . "_config/db.php");
$dbConfig = new config_db();
$db = $dbConfig->init();
$delete_coupongroup = $_GET['delete-coupongroup'];
if (isset($_GET['delete-coupongroup'])) {
$stmt = $db->prepare("DELETE FROM `product_onetime` WHERE groupid = $delete_coupongroup");
$stmt->execute();
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
?>
this is the product_onetime table
What I see is that my code seems to look like they are connected, but the delete button does not work. I tried to manually do the SQL in phpmyadmin, for example:
DELETE FROM `product_onetime` WHERE groupid = 5
Doing it manually works, but the php/sql does not. Would like some input.
My guess is that the file clinics_buttons.vc.php is not executed at all, because the code we see in the question is inside a <form>. In this case, the button behavior overrides the link behavior and the browser will submit the form instead of following the link.
You should be able to see that in the network tab of your development tools in your browser.
As a quick fix, change the button from a submit button into a push button:
<button type="button" class="btn btn-danger btn-sm full_width" data-toggle="modal">
<i class="fa fa-edit"></i>DELETE
</button>
A better solution would be not to use a button at all. Just make the link look like a button by using CSS styling. Or alternatively, remove the code from outside the outer form tag and give it its own form using clinics_buttons.vc.php as the form action.

two submit button inside one form in codeigniter

hi i want to have two submit button in one form in code igniter. is it possible? i want to make two buttons which will do the same thing, add data into the database, the only difference of the buttons is on which page they will redirect.
here is my codes,
<?php
echo form_open('EmpFamilyInfo/add_childinfo/'.$this->uri->segment(3));
?>
// some textboxex,
<div class="box-body">
<div class = 'col-md-6 col-sm-offset-6'>
<button class="btn btn-info fa fa-save" type="submit">&nbsp Save</button>
<a href = '<?php echo base_url().'EmpFamilyInfo/other_childinfo/'.$this->uri->segment(3); ?>' class = 'btn btn-primary fa fa-save' >&nbsp Add Another</a>
<a href = '<?php echo base_url().'EmpFamilyInfo/parentsinfo_father/'.$this->uri->segment(3); ?>' class = 'btn btn-danger fa fa-arrow-circle-right'>&nbsp Skip</a>
<?php
echo form_close();
?>
</div>
</div>
i have made this code which the first link echo form_open('EmpFamilyInfo/add_childinfo/'.$this->uri->segment(3)); what i wanted this to do is
public function other_childinfo(){
$this->form_validation->set_rules('NAME', 'Name of Child' ,'trim|required|max_length[100]');
if($this->form_validation->run($this) == FALSE){
$this->child_info();
}else{
if($query = $this->EmpFamilyInfo_Model->insert_childinfo()){
redirect('EmpFamilyInfo/child_info/'.$this->uri->segment(3));
}else{
$this->child_info();
}
}
}
but the error is,that it does not have post data. how can i make this link a submit button but it will go to different function or how can i make it have the post data?
this question ahs been answered already on this site, so this response is not mine, just quoting for you:
This uses javascript so keep in mind users who have this disabled will not be able to use this solution.
<script type="text/javascript">
function submitForm(action)
{
document.getElementById('form1').action = action;
document.getElementById('form1').submit();
}
</script>
...
<input type="button" onclick="submitForm('page1.php')" value="submit 1" />
<input type="button" onclick="submitForm('page2.php')" value="submit 2" />

How to use PHP variable value in javascript?

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>

Categories