Header location input error - php

I have problems with custom search input that redirects to id:
mysite/search/$id.
The thing is that is working, but.. when I'm at this site:
http://mysite/search/$id after entering again into input some id it display it as mysite/search/search
How can I solve it?
The code I use
$name = $_POST['name'];
header("Location: search/$name");
HTML:
<form method="post" action="search"> <input type="text" name="name"> <input type="submit"> </form>

Where is $id defined? It looks like you want
header("Location: search/$name");
EDIT:
After seeing your form, you'll want to change your PHP to:
$name = $_POST['name'];
header("Location: search/" . $name );
EDIT:
I have tested this and it works. Your action in the form is what's causing the issue. Change your code to this
<?php
if(isset($_POST['name'])){
$name = $_POST['name'];
header("Location: /search/$name");
}
?>
<form method="post"> <input type="text" name="name"> <input type="submit"> </form>

Related

Can not find why POST method not passing some html codes

I am using Post form, which works perfectly with any text, but when try to post code like format=xhtml, redirect me to 404
I am on this already all night and can not solve it with google`s help.
It is PHP page
Works on the example page
https://tryphp.w3schools.com/showphp.php?filename=demo_form_post
Does not work on my server
I am posting
content="format=xhtml; url=https://m.1688.com/offer/42866153932.html" />
Here is the example code
<!DOCTYPE HTML>
<html>
<body>
<form action="test45.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
<?php
//include "fetchpage.php";
?>
<?php
echo "Welcome&nbsp";
echo $_POST['name'];
echo "<br> Your e-mail address is:$nbsp";
echo $_POST['email'];
?>
You need to understand php form handling first.
Please go through this link php form handling
OR
Just see the code below..
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-Mail: <input type="text" name="email"><br>
<input type="submit">
</form>
AND PHP CODE SHOULD LOOK LIKE THIS...
<?php
echo "Welcome&nbsp";
echo $_POST['name'];
echo "<br> Your e-mail address is:&nbsp";
echo $_POST['email'];
?>
Get it run on: CLICK HERE...
HOPE IT HELPS...

Storing form data in session with php

I'm testing a really basic PHP form, where the form data is saved in a session.
Later, i want that session data to be the default value of the form:
<strong>Test Form</strong>
<form action="" method"post">
<input type="text" name="var" value=<?php $name ?>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
// starting the session
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['var'] = $_POST['var'];
$name = $_SESSION['var'];
}
echo $name;
?>
So, for example if i input "MyName" it should echo "MyName" and in the form there should be the value "MyName". The problem with the actual code is that it gives an E_NOTICE : type 8 -- Undefined variable: name -- at line 18 error. I think that the variable is not being stored, can someone help me out on this?
The first error I notice is this piece of code:
<form action="" method"post">
Where method does not contain the symbol "=" which causes the loss of the parameter post.
Furthermore, the "session_start ()" function must be placed before any other code. The code derives from this is as follows:
<?php
// starting the session
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['var'] = $_POST['var'];
$name = $_SESSION['var'];
} else {
$name = null;
}
?>
<strong>Test Form</strong>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="var" value="<?= ($name != null) ? $name : ''; ?>">
<input type="submit" name="Submit" value="Submit!" />
</form>
Spotted a few things:
I'd put the session_start(); at the top of the page before outputting anything.
Your method was incorrectly written it needs a '=' when specifying the method - thats the main reason why nothing was being stored, the form wasn't submitting properly.
Same with how you've put in the value on the name input - it has no '=' and you don't close the input tag properly - I've left it blank and added a placeholder - you can change it to what you need.
Heres how I'd do it:
<?php session_start(); ?>
<strong>Test Form</strong>
<form action="" method="post">
<input type="text" name="var" value="" placeholder="enter name">
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
if (isset($_POST['Submit'])) {
$_SESSION['var'] = $_POST['var'];
}
// Store the session in a variable after the submit - otherwise it will be forgotten on refresh
$name = $_SESSION['var'];
// check if session exists
if(isset($name)) {
echo $name;
}
else {
echo 'no name entered...';
}
?>
You can edit the above to hide the form if a name has been submitted etc. Use session_destroy(); to reset the stored session.
Cheers

$_Post Doesnt Work [Undefined index]

Giris.php (login screen)
<html>
<form method="post" action="verify.php">
<p>Birinci Isletim Sistemi:</p> <input type="text" name="txt_biris"><br><br>
<p>Ikinci Isletim Sistemi:</p> <input type="text" name="txt_ikis"><br>
<input type="submit" value="Karşılaştır!">
</form>
</body>
</html>
Verify.php
<?php
$ad = $_POST["txt_biris"];
echo"Ad...: $ad";
$sifre = $_POST["txt_ikis"];
echo "Sifre...: $sifre";
It returns..
Error
I tried many ways, controlled variables etc. But i didnt fix it. I'm using PHP 7 on Apache 2.4.17.
Use
<?php
if(isset( $_POST["txt_biris"]))
{
$ad = $_POST["txt_biris"];
echo"Ad...: $ad";
$sifre = $_POST["txt_ikis"];
echo "Sifre...: $sifre";
}

How can I write the data entered in a form to a text file on button click?

I'll preface this with I'm not a coder nor aspiring to become one.
I just want to play around with something simple.
Please don't feel bad about spoon-feeding me here haha.
All I want is when I hit a my submit button the text entered in the text field is saved to a file called log.text
I want it to overwrite each time.
Once data has been written I want it to redirect to another page.
Tried this but it doesn't create the file nor write to it even if I create it manually. The redirect also doesn't work because I'm an idiot.
Any help guys? :(
<?php
$email = $_REQUEST['email'];
$file = fopen("log.txt","a+");
fwrite($file,$email);
print_r(error_get_last());
header("Location: http://www.example.com/");
?>
<form action= "" method="post" name="form">
<input type="text" name="email">
<br>
<br>
<input type="submit" name="submit" value="submit"><br>
</form>
It is because the action element of the form is empty.
It should be \n
<form action="action.php(or any other php file that is handling the form)" method="post" name="form">
This guide offered me the solution I was after.
Thanks anyway guys!
http://www.howtoplaza.com/save-web-form-data-text-file
Because you dint checked whether the form is submitted or not. if submited create log. code given below
<?php
if(isset($_REQUEST['submit']))// if to check whether submit name is passed or not
{
$email = $_REQUEST['email'];
$file = fopen("log.txt","a+");
fwrite($file,$email);
print_r(error_get_last());
header("Location: http://www.example.com/");
}
?>
<html>
<form action= "" method="post" name="form">
<input type="text" name="email">
<br>
<br>
<input type="submit" name="submit" value="submit"><br>
</form>
</html>

Session variables from form not showing on next page

I can set session variables and use them on another page. However when i try to use a simple contactform with a username and email address and try to store them into session variables, they don't show up on other pages. There must be something basic i'm missing.
Here's the form:
<?php
session_start();
$submit = $_POST["submit"];
if($submit){setSessionVars();}
function setSessionVars() {
$_SESSION['name'] = $_POST['name'];
$_SESSION['email'] = $_POST['email'];
header('Location: session.php');
}
?>
<html>
<body>
<form action="session.php" method"post">
<input name="name" type="text" value="Name" size="11" /><br />
<input name="email" type="text" value="Email" size="11" /><br /><br />
<input name="submit" type="submit" value="Submit" size="11" />
</form>
</body>
</html>
And this is session.php:
<?php
session_start();
echo $_SESSION['name'];
echo $_POST['name'];
?>
Also
header('Location: session.php');
is not working. Any ideas?
At a glance, I see one immediate problem that will keep the form from posting.
<form action="session.php" method"post">
You need an "=" sign between method and "post".
Changing that alone will give you the "t" in session.php.
You post the form to session.php:
<form action="session.php" method"post">
I'd change it to:
<form method="post">
That way, the page posts to itself. Then it can register the session variables and redirect the user to session.php.
Edit: also, you forgot the = sign in method"post".

Categories