I've written this very simple bit of code. but when I click log out, nothing should happen to the session, yet it is no longer displayed.
please help me understand why.
thanks.
<?php
echo <<<_END
<form method="post" action="">
<input type="hidden" name="in" value="yes" />
<input type="submit" value="Log in" /> </form>
<form method="post" action="">
<input type="hidden" name="out" value="yes" />
<input type="submit" value="Log out" /> </form>
_END;
if(isset($_POST['in']))
{
session_start();
echo "hello, logged in!";
}
if (isset($_POST['out']))
{
echo "logged out";
}
echo session_id();
?>
You can't have a session ID if you don't start the session.
Related
So i have the following code for the aside which allows the user to search for other user(NOTE: i have left out the aside-tag here in order to make the code more readable):
<form action="index.php" method="POST">
<input id="search" type="text" placeholder="Search for Friends" name="search_name">
<input class="submit" type="submit" name="search-submit" value="Search">
</form>
After than i get the data from the database and display them in a variable whicht is called $output:
<?php
echo #$output;
?>
Ok so now when the user clicks on a name it should load the chatwindow in the aside instead of the search results. So the $output should be replaced with the chat window which has the following code:
<?php
if (isset($_POST['chat-submit'])) {
echo '<form method="POST" action="index.php" class="form-back">
<input class="back" type="submit" name="back" value="←Back">
</form>
<form>
<div class="chatwith"><h1><?php echo $username; echo ":"; ?></div>
<?php echo $finalstatus; ?>
<form action="index.php" method="POST" class="message-form">
<input class="message" type="text" placeholder="message" name="message">
<input class="submit" type="submit" name="message-submit" value="Send">
</form>';
}
?>
ok so both codes are working fine but when i click on one of the $outputs (and activate "chat-submit") than the old form from the search box is still there. How can i remove the old form so that only the chatwindows will be shown. Is there some kind "refresh only the aside"? If you have any questions please ask me.
If you set $refresh to 0 you dont need the if. Because it is always 0. Do like this:
<?php
if(isset($_POST['chat-submit'])) {
echo '//here goes the chat-form but this is too long for this comment. But its the same as above'; }
else {
echo $output;
}
?>
So it does work now this is the right solution from #Dieter Kräutl:
<?php
if(isset($_POST['chat-submit'])) {
echo '<form method="POST" action="index.php" class="form-back">
<input class="back" type="submit" name="back" value="←Back">
</form>
<form>
<div class="chatwith"><h1><?php echo $username; echo ":"; ?></div>
<?php echo $finalstatus; ?>
<form action="index.php" method="POST" class="message-form">
<input class="message" type="text" placeholder="message" name="message">
<input class="submit" type="submit" name="message-submit" value="Send">
</form>'; }
else {
echo $output;
}
?>
I have a site where I make a payment, a bill is created through it, but the problem is that I can not use $ _POST twice in one, and this example:
<? if (isset($_POST['submit'])) { ?>
<form action="" method="POST" >
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>
<? } if (isset($_POST['pay'])) {
// MY QUERY HERE
// HEADERS HERE
} else { ?>
<form action="" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
<? } ?>
Try this.
Kindly check the code for comment.
<?
if (isset($_POST['submit'])) {
$info = $_POST['info'];
// use echo to display second form
echo '
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<!-- // Note the action value, it's for post back. This allow you to post back to the current page -->
<!-- This is to keep the record passed from the first form -->
<input name="info" value="'. $info .'" type="hidden" />
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>';
} else if (isset($_POST['pay'])) {
// MY QUERY HERE
} else {
// Note the action value, it's for post back. This allow you to post back to the current page
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
}
?>
Not the prettiest way to do this but to achieve your result try this...
<?php if (isset($_POST['submit'])) { ?>
<form action="" method="POST" >
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>
<?php } elseif (isset($_POST['pay'])) {
// Perform query here
} else { ?>
<form action="" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
<?php } ?>
I coded the log-in page as follows
<!DOCTYPE html5>
<html>
<head>
<? if (isset($_REQUEST['page1_submit'])) {
if ($_REQUEST['pword'] == "meatball") { ?>
<center><h2>You entered the correct password on the first try!</h2></center>
<? } else { ?>
<center><h2>Wrong Password! Try again.
<form method="POST" action="password.php">
Password: <input type="password" NAME="pword" /><br /><br />
<input type="submit" name="page2_submit" value="SUBMIT" />
<input type="reset" value="RESET" /><br />
</form>
</h2></center>
<? } ?>
<? } elseif (isset($_REQUEST['page2_submit'])) {
if ($_REQUEST['pword'] == "meatball") { ?>
<center><h2>You entered the correct password! But it took you two tries.</h2></center>
<? } else { ?>
<center><h2>Wrong Password Again! This was your second try.
<form method="POST" action="password.php">
Password: <input type = "password" name="pword" /><br /><br />
<input type="submit" name="page3_submit" value="SUBMIT" />
<input type="reset" value="RESET" /><br />
</form>
</h2></center>
<? } ?>
<? } elseif (isset($_REQUEST['page3_submit'])) {
if ($_REQUEST['pword'] == "meatball") { ?>
<center><h2>You entered the correct password! But it took you three tries.</h2></center>
<? } else { ?>
<center><h2>Wrong Password Again! You're out of luck.</h2></center>
<? } ?>
<? } else { ?>
<!-- This is the first pass for the user – page 1 -->
<center><h2>Please enter your password to access this site!<br />
<form method="POST" action="password.php">
Password: <input type="password" name="pword"><br /><br />
<input type="submit" name="page1_submit" value="SUBMIT" />
<input type="reset" value="RESET" /><br />
</form>
</h2></center>
<? } ?>
</body>
</html>
When I run this php, everything is appearing on the same page while password function is not properly working.
I even followed the guideline that our professor provided but can't find any clear solution to this..
In PHP some times php.ini is set to not allow short code.
<? is short for <?PHP if your server is not set to read <? then it will look the way it does. Try Adding <?PHP to all <? short codes.
If the server cant read the php starts and ends then it will just run them all together.
EX:
<?PHP
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<?PHP if (isset($_REQUEST['page1_submit'])) {
if ($_REQUEST['pword'] == "meatball") { ?>
<center><h2>You entered the correct password on the first try!</h2></center>
<?PHP } else { ?>
<center><h2>Wrong Password! Try again.
<form method="POST" action="password.php">
Password: <input type="password" NAME="pword" /><br /><br />
<input type="submit" name="page2_submit" value="SUBMIT" />
<input type="reset" value="RESET" /><br />
</form>
</h2></center>
<?PHP } ?>
Notice all the <?PHP code starts
PLEASE DO NOT CODE LIKE THIS THIS IS TERRIBLE AND YOUR INSTRUCTOR NEEDS A LESSON ABOUT CLEAN CODE
http://php.net/manual/en/language.basic-syntax.phptags.php
can we use $_POST 2 times with different page ?
this example ..
action.php
<form action="next.php" method="post">
<input name="test" value="test" />
<button type="submit" name="test">submit</button>
</form>
next.php
<?php
$test=$_POST['test'];
?>
<form action="next1.php" method="post">
<input name="test1" value="<?php echo $test; ?>" />
<button type="submit" name="test1">submit</button>
</form>
next1.php
<?php
echo $test2=$_POST['test1']; >> in here i didnt get test1 value . why ?
?>
Within next.php and action.php you need to change your input type like as
<input type="text" name="test" value="test" />
<input type="text" name="test1" value="<?php echo $test; ?>" />
Within next.php and action.php you were missing the type attr of input tag
and you have same name attributes for submit and input within next.php need to remove or change the value from respective
<input type="submit" value="submit"/>
Below code i have tried my local server and it executed successfully.
action.php:-
<form action="next.php" method="POST">
<input type="text" name="test"/>
<input type="submit" value="submit"/>
</form>
next.php:-
<?php
$test = $_POST['test'];
echo $test;
?>
<form action="next1.php" method="POST">
<input name="test1" value="<?php echo $test; ?>" />
<input type="submit" value="submit"/>
</form>
next1.php:-
<?php
$test2=$_POST['test1'];
echo "------".$test2;
?>
Hope this will help.I think this is achieved your requirement.
If you want to do it within one file e.g. index.php then here is the code
<?php
if(isset($_POST['test'])){
if(isset($_POST['test_text'])){
echo 'Output from NEXT: '.$_POST['test_text'];
}
}
elseif(isset($_POST['test1'])){
if(isset($_POST['test_text1'])){
echo 'Output from NEXT1: '.$_POST['test_text1'];
}
}
?>
<table style="width:100%;">
<tr >
<td style="width:50%;">
<form action="" method="post" name="next">
<input type="text" name="test_text" value="<?php echo isset($_POST['test_text']) ? $_POST['test_text']:''; ?>" />
<button type="submit" name="test">submit test1</button>
</form>
</td>
<td style="width:50%;">
<form action="" method="post" name="next1">
<input type="text1" name="test_text1" value="<?php echo isset($_POST['test_text1']) ? $_POST['test_text1']:''; ?>" />
<button type="submit" name="test1">submit test1</button>
</form>
</td>
</tr>
</table>
I hope this will help.
You need to send the POST value with the second form again, e.g. in a hidden field, otherwise the value won't be sent.
Furthermore your submit button shouldn't have the same name as the input field you want the content from. So change the name of your submit button, e.g.:
action.php
<form action="next.php" method="post"> <input name="test" value="test" /> <button type="submit" name="submit">submit</button> </form>
next.php
<form action="next1.php" method="post">
<input name="test1" value="<?php echo $test; ?>" />
<button type="submit" name="submit">submit</button>
</form>
I want to make it to so if my form is submitted then then the checkbox should be checked.
Here is what I have:
<input type="checkbox" id="chk" <?php if ($_POST['submit']){ echo checked } ?> />
HTML:
<form id="form1" enctype="multipart/form-data" action="" method="post">
//Stuff here
<input name="submit" type="submit" value="Upload" />
</form>
I am getting a blank page when I test it... Any help will be appreciated.
Blank is because there is an php error of your php code and you have turned error_reporting off.
if($_POST['submit']) should be if (isset($_POST['submit']))
echo checked should be echo 'checked';
The sample below works for me.
<form id="form1" enctype="multipart/form-data" action="" method="post">
//Stuff here
<input name="submit" type="submit" value="Upload" />
</form>
<input type="checkbox" id="chk" <?php if (isset($_POST['submit'])){ echo 'checked'; } ?> />
There is an error in your PHP code.Please correct it.
<input type="checkbox" id="chk" <?php if ($_POST['submit']){ echo 'checked'; } ?> />