How to perform same event on two different buttons? - php

I have this piece of code below, supposing it will display no request when no data in database and it will display the request with an accept and reject button when they is data in database. How should i write my code in order to make the accept and reject button perform some action?
<?php
$travelRequest = $user->userTravelRequest($userid);
if(!$travelRequest){
echo '<div class="requestbox">
<p> You have no request from others at the moment. </p>
</div>';
}
else {
foreach($travelRequest as $request){
echo '<div class= "requestbox">
<p>'. $request->trip_name.'</p>
<p>Organised by '.$request->username.'</p>';
?>
<form method="POST">
<div class = "AR-btn">
<input type="button" name="accept" value="Accept"/>
<input type="button" name="reject" value="Reject"/>
<p></p>
</div>
</form>
</div>
<?php
}
}
?>

Use type="submit" instead of type="button", and give them the same name. Then they'll submit the form, and the script can test which button was used from the value of that parameter.
<input type="submit" name="action" value="Accept"/>
<input type="submit" name="action" value="Reject"/>
You then test this with:
if (isset($_POST['action'])) {
if ($_POST['action'] == 'Accept') {
// Add to database
} elseif ($_POST['action'] == 'Reject') {
// Delete from database
}
}

Try this code :-
<?php
if (isset($_POST['accept']) && $_POST['accept'] == 'Accept' ) {
// do what you want
die("Accept is clicked");
} else if (isset($_POST['reject']) && $_POST['reject'] == 'Reject' ) {
// do what you want
die("Reject is clicked");
}
?>
<?php
$travelRequest = $user->userTravelRequest($userid);
if(!$travelRequest){
echo '<div class="requestbox">
<p> You have no request from others at the moment. </p>
</div>';
} else {
foreach($travelRequest as $request){
echo '<div class= "requestbox">
<p>'. $request->trip_name.'</p>
<p>Organised by '.$request->username.'</p>';
?>
<form method="POST">
<div class = "AR-btn">
<input type="submit" name="accept" value="Accept"/>
<input type="submit" name="reject" value="Reject"/>
<p></p>
</div>
</form>
</div>
<?php
}
}
?>

Just try this logic..may help you
$query = "select data from database";
.
.
.
$data = $row['data'];
if($data){ // reject button when they is data in database
echo '<input type="submit" name="action" value="Reject"/>';
}
else{ // viceversa
echo '<input type="submit" name="action" value="Accept"/>';
}

Related

PHP - Validate if textbox has a value or not?

I want to validate if the textbox has a value or not. Right now what I have is a textbox that has a value but the output says it is empty here is it it is like nothing is being conditioned on the code please see me code, thank you
Full Code
-Here is the full code of my form please take a look thank you very much
<form>
<div class="row">
<form method="POST">
<div class="col-md-8">
<?php
$code = 'Code';
$code2 = 'PIN';
if(isset($_POST['btnSubcode'])) {
$lblCode = isset($_POST['lblQrTxt']) ? $_POST['lblQrTxt'] : '';
$code = $lblCode;
$code = explode(":",$code); // code = array("QR Code","444444444|123")
$code = explode("|",$code[1]); // code[1] = "444444444|123"
$code = trim($code[0]); // 444444444
$code2 = $lblCode;
$code2 = explode(":",$code2); // code = array("QR Code","444444444|123")
$code2 = explode("|",$code2[1]); // code[1] = "444444444|123"
$code2 = trim($code2[1]); // 123
}
?>
<div class="form-group">
<label class="form-control-label">code</label>
<input type="text" name="input" id="card-code" value='<?php echo $code ?>' class="form-control">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="form-control-label">pin</label>
<input type="text" id="card-pin" value='<?php echo $code2 ?>' class="form-control" maxlength="3">
</div>
<?php
if(isset($_POST['txtQrtxt']) && $_POST['txtQrtxt'] != '') {
echo "Text Present";
} else {
echo "Text Not Present";
}
?>
<div class="caption">
<div class="jumbotron">
<input type="text" name='txtQrtxt' value='Hello World' class="form-control" >
<textarea class="form-control text-center" id="scanned-QR" name="lblQrTxt"></textarea><br><br><br>
</div>
</div>
</form>
<div class="form-group float-right">
<input value="Topup" class="btn btn-primary topup-button">
</div>
</div>
</div>
</form>
<?php
$txtCodeqr = isset($_POST['txtQrtxt']) ? $_POST['txtQrtxt'] : '';
if (!empty($txtCodeqr)) {
echo "Text";
} else {
echo "Empty Textbox";
}
?>
my textbox
<input type="text" name='txtQrtxt' value='Hello World' class="form-control" >
You might be over complicating it. It is pretty simple.
<?php
if(isset($_POST['txt']) && $_POST['txt'] != '') {
echo "Text Present";
} else {
echo "Text Not Present";
}
?>
Additionally I would recommend you filter all input on post or get. Basically anything that gets information from a user.
Check here - http://php.net/manual/en/function.filter-input.php
<?php
$my_txt = filter_input(INPUT_POST, 'txt');
if(isset($my_txt) && $my_txt != '') {
echo "Text Present";
} else {
echo "Text Not Present";
}
?>
Also you need to add a submit button between your form tags. Like this.
<input type="submit" value="Submit">
Also you should have only one closing tag for every opening tag. This is called valid HTML.
For example a valid form is like
<form method="post">
First name:<br>
<input type="text" name="firstname" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse"><br><br>
<input type="submit" value="Submit">
</form>
Ok I have made a simple php test file and tested it works. Your problem is:
You don't have a submit button. The $_POST will not be there if you do not submit a form first.
It would be easier to validate your textarea using javascript instead.
Here is my test file and it works:
<html>
<body>
<form method="POST">
<textarea name="txtQrtxt">
</textarea>
<input type="submit">
</form>
<?php
$var = $_POST['txtQrtxt'];
if (strlen($var)<=0) {
echo "Textarea empty";
} else {
echo "Textarea Okay";
}
?>
</body></html>

i am using two submit button one form in codeigniter it was worked well in chrome but iam using the firebox both button going to the else part

<body>
<div id='container'>
<pre>
<fieldset>
<legend>login</legend>
<?php echo validation_errors(); ?>
<form method="post" id="login" name="login">
username :<input type="text" name="uname" id="uname" value="<?php echo $this->session->flashdata('uname') ?>" ><br>
password :<input type="password" name="upass" id="upass">
<input type="checkbox" name="remember">Remember me
<input type="submit" value="login" class="submit" name="login"> <input type="submit" value="signup" name="signup">
</form>
</fieldset>
</pre>
<div><?php echo $this->session->flashdata('error'); ?></div>
</div>
<div id="error"></div>
</body>
controller:
public function view()
{
if($this->input->post("login"))
{
if($this->input->post('remember')=='on')
{
$uname=$this->input->post("uname");
$this->session->set_flashdata('uname',$uname);
}
$this->form_validation->set_rules('uname','username','trim|required');
$this->form_validation->set_rules('upass','password','required');
$data['title']='login page';
if ($this->form_validation->run() === FALSE)
{
$this->session->set_flashdata('error','enter username and password');
$this->load->view('stud_det/login',$data);
}
else
{
$data['result']=$this->stud_model->login();
if(!empty($data['result']))
{
$session=array(
'uname'=>$this->input->post('uname'),
'upass'=>$this->input->post('upass')
);
$this->session->set_flashdata($session);
$this->session->set_userdata($session);
$this->load->view('stud_det/view',$data);
}
else{
$this->session->set_flashdata('error','username or password is incorrect');
$this->load->view('stud_det/login',$data);
}
}
}
else{
$this->load->view('stud_det/index',array('title'=>''));
}
In your first if condition, you are loking for 'login' POST variable, however 'login' is the name of the submit input type. You have to test the post value 'uname' and 'upass'. Something like this:
if($this->input->post("uname") && $this->input->post("upass")) {
// Your code...
}
Other thing, you do not need define name to form tag. The name 'login' it is duplicated in form and input tags.

How to show a success message after PHP form submission?

Here is the code. I want it this way --
Form submission --> page2.php --> redirect --> page1.php (Here is the message. Pop-up or whatever)
page1.php
<form action="page2.php" method="post" enctype="multipart/form-data" class="form-inline subscribe-form">
<input type="name" name="name" placeholder="Jack">
</div>
<button type="submit" name="sub" value="sub" >Submit</button>
</form>
page2.php
<?php
//include necessary
if(isset($_POST['sub'])) {
$nameget = mysqli_real_escape_string($dbconnect, $_POST['name']);
$sqlentry = .....bla bla......//insert into DB
}
$getsql = mysqli_query($dbconnect, $);
if($getsql){
mysql_close($dbconnect);
header('location:page1.php');
}
?>
Where you have:
header('location:page1.php');
append a variable on the location, like:
header('location:page1.php?status=success');
And on page1.php, do something like:
if( $_GET['status'] == 'success'):
echo 'feedback message goes here';
endif;
This way your flash message will not show up again and again after refresh.
<?php session_start();
if(isset($_SESSION['msg']) && $_SESSION['msg'] != ''){
echo $_SESSION['msg'];
unset($_SESSION['msg']);
}
?>
<form action="page2.php" method="post"
enctype="multipart/form-data" class="form-inline subscribe-form">
<input type="name" name="name" placeholder="Jack">
</div>
<button type="submit" name="sub" value="sub" >Submit</button>
</form>
And
<?php
session_start();
//include necessary
if(isset($_POST['sub'])) {
$nameget = mysqli_real_escape_string($dbconnect, $_POST['name']);
$sqlentry = .....bla bla......//insert into DB
}
$getsql = mysqli_query($dbconnect, $);
if($getsql){
mysql_close($dbconnect);
$_SESSIOM['msg'] = 'Value Inserted or whatever';
header('location:page1.php');
}
?>

language change in php

i am trying to change the all page language but session is not working . Session is taking previous value and language change after two click so please solve this problem.
$_SESSION['lanuage'] = $_POST['hindi_hidden'];
if (!empty($_SESSION['lanuage'])){
if ($_SESSION['lanuage'] =='hi')
{
$_SESSION['lanuage'] = $_POST['hindi_hidden'];
}
else if ($_SESSION['lanuage'] =='en')
{
$_SESSION['lanuage'] = $_POST['hindi_hidden'];
}
else {}
}
else
{}
<input type="button" name="hi" id="hi" value="Hindi" onclick="get_hindi(this.id);" class="submit_sytle"/>
<input type="button" name="en" id="en" value="Eng" onclick="get_hindi(this.id);" class="submit_sytle"/>
Can you try this
<?php
session_start();
if(isset($_POST['hindi_hidden'])){
$_SESSION['lanuage'] = $_POST['hindi_hidden'];
} else {
$_SESSION['lanuage']='hindi';
}
echo $_SESSION['lanuage'];
?>
<style>
.submit_sytle{
text-transform:capitalize;
}
</style>
<form method="post">
<input type="submit" name="hindi_hidden" id="hi" value="hindi" onclick="get_hindi(this.id);" class="submit_sytle"/>
<input type="submit" name="hindi_hidden" id="en" value="english" onclick="get_hindi(this.id);" class="submit_sytle"/>
</form>
Code after your comment:
You can try this, it works for me:
<?php
session_start();
if (isset($_POST['hindi_hidden'])) {
$_SESSION['lanuage'] = $_POST['hindi_hidden'];
}
if (isset($_SESSION['lanuage']) && ($_SESSION['lanuage'] =='hi' || $_SESSION['lanuage'] =='en')) {
$_SESSION['lanuage'] = $_POST['hindi_hidden'];
}
echo $_SESSION['lanuage'];
?>
<style>
.submit_sytle{
text-transform:capitalize;
}
</style>
<form method="post">
<input type="submit" name="hindi_hidden" id="hi" value="hindi" onclick="get_hindi(this.id);" class="submit_sytle"/>
<input type="submit" name="hindi_hidden" id="en" value="english" onclick="get_hindi(this.id);" class="submit_sytle"/>
</form>
<script>
function get_hindi(lang) {
//alert (lang);
document.getElementById('hindi_hidden').value=lang;
document.form_login.submit();
}
</script>
Output:
english when I click on English
hindi when I click on Hindi

php Post txt file content to textarea on another page

I am writing an app to delete and modify files in a directory. I deleted files successfully. And now I want to modify and update content to the same file using textarea on another page.
<form enctype="multipart/form-data" method="post">
<div class="span7">
<table>
<thead>
<tr>
<th> List of files </th>
<tr>
</thead>
<?php
$files = glob("UploadFile/"."*");
foreach($files as $txt)
{
if(mime_content_type($txt)=="text/plain")
{
$txtname = basename($txt);
echo "<tr><td><input type='radio' name='txt' value='$txtname'/> ".$txtname."</td></tr>";
}
}
?>
</table>
</div>
<div class="span8">
<button type="submit" name="submit" value="Edit">Edit</button>
<button type="submit" name="submit" value="Delete">Delete</button>
</div>
<?php
global $txtname;
$val = $_POST["submit"];
$txtname = $_POST["txt"];
if($val=="Delete")
unlink("UploadFile/".$txtname);
else if($val=="Edit")
{
$content=file_get_contents("UploadFile/".$txtname);
header('Location:/edit.php');
/* send content to this 'edit.php' page */
}
?>
</form>
And the edit.php page, i simply checked if the code is working. It works fine.
<?php
if($_POST['append'])
{
$file_open = fopen("UploadFile/file.txt","a+");
fwrite($file_open, $_POST['append']);
fclose($file_open);
}
?>
<form enctype="multipart/form-data" action="<?=$PHP_SELF?>" method="post">
<textarea name="append" value="">
<?php
echo $content;
$datalines = file ("UploadFile/file.txt");
foreach ($datalines as $zz)
{
echo $zz;
}
?>
</textarea>
<button type="submit" name="submit" value="save"> Save </button>
How do I get the contents of the file to textarea for modifying it.Update: I want to save changes to the same file. Thank you
<?php
if(isset($_POST['text']))
{
file_put_contents("file.txt",$_POST['text']);
header("Location: ".$_SERVER['PHP_SELF']);
exit;
}
$text = file_get_contents("file.txt");
?>
<form method="post">
<textarea name="text"><?=$text?></textarea>
<input type="submit">
</form>
with listing
if(isset($_POST['text']))
{
file_put_contents("UploadFile/".basename($_POST['file']),$_POST['text']);
header("Location: ".$_SERVER['PHP_SELF']);
exit;
}
if(isset($_GET['file']))
{
$text = file_get_contents("UploadFile/".basename($_GET['file']));
?>
<form method="post">
<input type="hidden" name="file" value="<?=urlencode($_GET['file'])?>">
<textarea name="text"><?=$text?></textarea>
<input type="submit">
</form>
<?
} else {
$files = glob("UploadFile/*");
foreach ($files as $f)
{
$f=basename($f);
?><?=htmlspecialchars($f)?><br><?
}
}

Categories