Refresh the aside without new page - php

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;
}
?>

Related

I would use $ _POST twice in one page

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 } ?>

can I use $_POST 2 times with different page?

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>

POST not working with no signs of error

haven't programmed PHP in a while but I
have to assemble something for a client really fast.
I've set up 2 forms with POST but when I go to the next file it's just blank space, for some reason POST isn't being registered but is set cause I'm not getting an error echo.
Hese's the forms:
<form action="Funkcije.php" method="post" name="AddFromDB">
<input type="text" placeholder="Šifra Art" name="ArtNo">
<input type="submit" value="Dodaj">
</form>
<br>
<div id="newItem">
<form action="Funkcije.php" method="post" name="AddNew">
<input type="text" placeholder="Šifra" name="Art">
<input type="text" placeholder="Ime Proizvoda" name="ImeProizvoda">
<input type="text" placeholder="Dobavljač" name="Dobava">
<input type="text" placeholder="Cijena" name="Cijena">
<input type="submit" value="Dodaj">
</form>
</div>
And here's the 2nd file:
if(isset($_POST["AddFromDB"], $_POST["ArtNo"])){
addExisting ($_POST["ArtNo"]);
}
else if(isset($_POST["AddNew"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
newItem ($_POST["Art"] && $_POST["ImeProizvoda"] && $_POST["Dobava"] && $_POST["Cijena"]);
}
else if (!isset ($_POST)){
echo "error";
}
So, by code I should be getting an error if POST is not set but I get nothing. Just a blank space.
here, you must be give a name to the submit button to check which form is POST like this...
<form method="post" name="AddFromDB">
<input type="text" placeholder="Šifra Art" name="ArtNo">
<input type="submit" value="Dodaj" name="form1">
</form>
<br>
<div id="newItem">
<form method="post" name="AddNew">
<input type="text" placeholder="Šifra" name="Art">
<input type="text" placeholder="Ime Proizvoda" name="ImeProizvoda">
<input type="text" placeholder="Dobavljač" name="Dobava">
<input type="text" placeholder="Cijena" name="Cijena">
<input type="submit" value="Dodaj" name="form2">
</form>
</div>
<?php
if(isset($_POST["form1"], $_POST["ArtNo"])){
echo "1";
}
else if(isset($_POST["form2"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
echo "2";
}
else{
echo "error";
}
?>
now, this work fine..
thank you.. enjoy coding...

php POST array not set

i am trying to collect data from the user in a form and display the data back to him .
i am using WAMP.
here is my html code
<FORM METHOD="POST" ACTION="submit.php">
<INPUT type="text" name="URL" size=17 value="http://">
<INPUT type="text" name="user" size=17>
<INPUT type="text" name="email" size=17>
<INPUT type="submit" value="Submit" name="submit"/>
<INPUT type=reset value="Clear">
</form>
here is my submit.php code
<?php
if (isset($_POST['URL'])){
echo "set";
}
else
{
echo "not set";
}
?>
when i execute this i am always getting "not set" as the output.
thanks.
It should be $_POST not $post, so your code should be :-
<?php
if (isset($_POST['URL'])){
echo "set";
}
else
{
echo "not set";
}
?>
Also your form tag should not contain encytype="text/plain" because PHP doesn't handle it (and it is not a bug)
Valid values for enctype in <form> tag are:
application/x-www-form-urlencoded
multipart/form-data
So remove encytype="text/plain"
<?php
if (isset($_POST))
{
echo "set";
}
else
{
echo "not set";
}
?>
try this
Remove
ENCTYPE="text/plain"
from the form
So the form should look like
<form method="POST" action="submit.php">
And its $_POST not $POST
if (isset($POST['URL'])){
should be
if (isset($_POST['URL'])){
Here is an example of handling the form
<?php
if(isset($_POST["submit"])){
if (isset($_POST['URL']) && $_POST['URL'] != ''){
echo "set";
}
else
{
echo "not set";
}
}
?>
<FORM METHOD="POST" ACTION="submit.php" >
<INPUT type="text" name="URL" size=17 value="http://">
<INPUT type="text" name="user" size=17>
<INPUT type="text" name="email" size=17>
<INPUT type="submit" value="Submit" name="submit"/>
<INPUT type=reset value="Clear">
</form>

understanding sessions

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.

Categories