how i can call the button inside the if statement PHP - php

I am a newbie I am having a little problem on how can i call the button inside my if statement. I am trying to work on a userlog where when you click a button it will output on the user log.
but how can i call the >>
THE VIEW button inside my if statement so that it will fwrite inside the userlog.txt.
inside this if($_POST['submit'] = ") same goes in the search button and the add edit delete button. did u get my point?
<body background="images.jpg">
<?php
session_start();
if($_SESSION['username'])
{
echo "Welcome, ".$_SESSION['username']."!<br><a href='logout.php'>Logout</a><br>";
echo '<FORM METHOD="LINK" ACTION="mydata4.php">
<INPUT TYPE="submit" VALUE="Edit/Delete/add">
</FORM>';
echo '<FORM METHOD="LINK" ACTION="mydata2.php">
<INPUT TYPE="submit" VALUE="View" id="viewbutton">
</FORM>';
echo '<FORM METHOD="LINK" ACTION="display_data.php">
<INPUT TYPE="submit" VALUE="Search">
</FORM>';
}
else
{
die("You must be logged in!");
}
if($_POST['submit'] = ")
{
$date=date("Y-m-d H:i:s");
$updatefile = "userlogs.txt";
$fh = fopen($updatefile, 'a') or die("can't open file");
$stringData = "User: $username click view button";
fwrite($fh, "$stringData".PHP_EOL);
fclose($fh);
}
?>

Use
if(isset($_POST))
instead of
if($_POST['submit'] = ")

try this... you are doing mistake in your if condition.
<body background="images.jpg">
<?php
session_start();
if($_SESSION['username'])
{
echo "Welcome, ".$_SESSION['username']."!<br><a href='logout.php'>Logout</a><br>";
echo '<FORM METHOD="LINK" ACTION="mydata4.php">
<INPUT TYPE="submit" VALUE="Edit/Delete/add">
</FORM>';
echo '<FORM METHOD="LINK" ACTION="mydata2.php">
<INPUT TYPE="submit" VALUE="View" id="viewbutton">
</FORM>';
echo '<FORM METHOD="LINK" ACTION="display_data.php">
<INPUT TYPE="submit" VALUE="Search" name="submit" id="submit">
</FORM>';
}
else
{
die("You must be logged in!");
}
if($_POST['submit'] == "submit")
{
$date=date("Y-m-d H:i:s");
$updatefile = "userlogs.txt";
$fh = fopen($updatefile, 'a') or die("can't open file");
$stringData = "User: $username click view button";
fwrite($fh, "$stringData".PHP_EOL);
fclose($fh);
}
?>

you can set the same name to your submit buttons ...
<input type="submit" name="submit" value="View" />
<input type="submit" name="submit" value="Search" />
and in your PHP, you can simply use as you mentioned
if( $_POST["submit"] == "View" )
...
if( $_POST["submit"] == "Search" )
...
one more things is that, you have to use method="post" in your <form> tag if you want to use $_POST in php
<form method="post" action="....php">

Related

Submit form data and go to next page on one click

Right now I have code that submits form data and gets put into a text file and then after I click a button to go to the next page. I want to be able to submit the form data and go to the next page on the same click. I'm using PHP and HTML right now. Is there anyway to successfully do this?? I tried an onclick like I have for my next button but it doesn't seem to work with the input type function.
<h2>Please enter your name.</h2>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
First Name: <input type="text" name="fName">
<br><br>
Middle Initial: <input type="text" name="mInitial">
<br><br>
Last Name: <input type="text" name="lName">
<br><br>
<input type="submit" name="submit" value="Submit">
<button type="button" name="submit" value="Submit"
onclick="location.href='county.php'">Next</button>
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['submit']))
{
replace();
}
function replace() {
$myFile = "freewill.doc";
$fh = fopen($myFile, 'a') or die("can't open file");
$fName = $_POST["fName"];
$mInitial = $_POST["mInitial"];
$lName = $_POST["lName"];
$placeholders = array('fin', 'min', 'lana');
$namevals = array($fName,$mInitial,$lName);
$path_to_file = 'freewill.doc';
$file_contents = file_get_contents($path_to_file);
$file_contents = str_replace($placeholders,$namevals,$file_contents);
file_put_contents($path_to_file,$file_contents);
fclose($fh);
}
?>
Change below line
<button type="button" name="submit" value="Submit"
onclick="location.href='county.php'">Next</button>
to
<input type="submit" name="submitform" value="Submit">Next</button>
and change PHP code to this
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['submitform']))
{
$myFile = "freewill.doc";
$fh = fopen($myFile, 'a') or die("can't open file");
$fName = $_POST["fName"];
$mInitial = $_POST["mInitial"];
$lName = $_POST["lName"];
$placeholders = array('fin', 'min', 'lana');
$namevals = array($fName,$mInitial,$lName);
$path_to_file = 'freewill.doc';
$file_contents = file_get_contents($path_to_file);
$file_contents = str_replace($placeholders,$namevals,$file_contents);
file_put_contents($path_to_file,$file_contents);
fclose($fh);
header('Location: county.php');
}
Try setting an onSubmit method for your form instead.

Php(sessions)...how to put textbox value in session and pass it to another page?i m getting error

<?php
session_start();
?>
<form action="HardDisk.php" method="post">//form
<input type="text" name="user" />
<?php
if(isset($_POST['user']))
{
$_SESSION['userGet']=$_POST['user'];
}
?>
<input type="submit" value="submit" />
</form>
2nd page
<?php
session_start();
?>
<?php
if (isset($_SESSION['userGet'])) {
$form_value = $_SESSION['userGet'];
}
echo $form_value ;
?>
..........................................................................................................
You have used the text box name with that check the submit button name too.
<form action="HardDisk.php" method="post">//form
<input type="text" name="user" />
<input type="submit" value="submit" name="submit" />
</form>
<?php
session_start();
if(isset($_POST['submit'])&&isset($_POST['user'])) {
$_SESSION['userGet']=$_POST['user'];
}
?>
And in your second page
<?php
session_start();
if (isset($_SESSION['userGet'])) {
echo $form_value = $_SESSION['userGet'];
}
Give the submit button a name of submit then use the following PHP code:
if(isset($_POST['submit'])){
if(isset($_POST['user']))
{
$_SESSION['userGet']=$_POST['user'];
}
}

How to perform same event on two different buttons?

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"/>';
}

Error-message in password is visible on pageload?

How do i prevent the error-message from showing before the 'submit'-button is clicked?
For some reason the content of 'else'-statement is visible on pageload - the message "NO" should only appear after typing something else than 'a' in this.. what am i doing wrong?
<form method="POST">
<input type="text" class="textfield" id="cursor" name="pass">
<input type="submit" class="button" name="submit" value="OK">
</form>
<?php
$pass = $_POST['pass'];
if($pass == 'a') {
echo "YES";
}
else{
echo "NO";
}
?>
Add this PHP code:
<?php
if(isset($_POST['pass'])) {
// your check here
}
?>
Test to see if the submit button is in the submitted data.
if (isset( $_POST['submit'] )) {
<form method="POST">
<input type="text" class="textfield" id="cursor" name="pass">
<input type="submit" class="button" name="submit" value="OK">
</form>
<?php
$pass = $_POST['pass'];
//You need to check if the form is submitted
if($_POST['submit']){
if(isset($pass) && !empty($pass) && $pass == 'a') {
echo "YES";
}
else{
echo "NO";
}
?>

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