Can't access post in php - php

Here's my code in a short way. This code can't access post and I don't know why.
if ($_POST) {
echo 'post a girdim';
}
?>
<html>
<head>
</head>
<body>
<form action="" method="post">
<input type="submit">
</form>
</body>

You don't have any elements in your form with a name. Therefore no values are submitted to the server, the $_POST array is empty and if ($_POST) is false.
If you don't want any named elements, you can check whether the request method in $_SERVER['REQUEST_METHOD'] is "POST".

set input name like:
<input name="submit" type="submit" />

Give your input tags names.
if (isset($_POST["submit"])) {
echo 'post a girdim';
}
?>
<html>
<head>
</head>
<body>
<form action="" method="post">
<input type="submit" name="submit">
</form>
</body>

you should follow this
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
and further
you can give name for input
for example
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" name="submit">
</form>
and then you must check this
<?php
extract($_POST);
if (isset($_POST['submit'])) {
echo $_POST['submit'];
}
?>

Related

Input from form to url?

So the goal is to take the input from a form append it to the end of the URL and then return the HTML from that page.
I am not entirely sure how to take the forms value (in this case $2) and attach it to the URL.
<html>
<head>
<title>Metar Test</title>
</head>
<body>
<form action="" method="POST">
<p>IACO Code: <input type="text" name="$2" value=""></p>
<input type="submit" name="submit" value="Submit">
</form>
<?php $html=file_get_contents("http://www.metar.mysite.net/metar?id=$2")?>
<?php echo $_POST ["$html"];?>
</body>
</html>
On submit any input from a form with method="POST" will be stored in the $_POST global. You can access it from there and append it to your URL string.
<html>
<head>
<title>Metar Test</title>
</head>
<body>
<form action="" method="POST">
<p>IACO Code: <input type="text" name="iacoCode" value=""></p>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST["iacoCode"])) {
$html = file_get_contents("http://www.metar.mysite.net/metar?id=" . $_POST["iacoCode"]);
echo $html;
}
?>
</body>
</html>
Using the IF statement to check if it is set will prevent it from loading the URL with no variable.
The way we extract data via php from a form is like below
<html>
<head>
<title>Metar Test</title>
</head>
<body>
<form action="" method="POST">
<p>IACO Code: <input type="text" name="$2" value=""></p>
<input type="submit" name="submit" value="Submit">
</form>
<?php $html=file_get_contents("http://www.metar.mysite.net/metar?id=$_POST['$2']")?>
<?php echo $_POST ["$html"];?>

Two form with submit button

I have two form with submit button in php file
when click on first submit button url become
http://url.com/?valueI=Want
when click second submit button: I need url become http://url.com/
without parameter
How i can do this please
<?php
if (isset($_POST['ASK_Button'])) {
}
if (isset($_POST['SET_Button'])) {
}
?>
<html>
<body>
<form id="ASKUser" class="blocks" action="#" method="post">
<input type="submit" Name="ASK_Button" value="ASK">
</form>
<form id="SETUser" class="blocks" action="#" method="post">
<input type="submit" Name="SET_Button" value="SET">
</form>
</body>
</html>
Change your ask form method to GET
<?php
if (isset($_GET['ASK_Button'])) {
}
if (isset($_POST['SET_Button'])) {
}
?>
<html>
<body>
<form id="ASKUser" class="blocks" action="#" method="GET">
<input type="text" name="valuel" value="Want">
<input type="submit" Name="ASK_Button" value="ASK">
</form>
<form id="SETUser" class="blocks" action="#" method="post">
<input type="submit" Name="SET_Button" value="SET">
</form>
</body>
</html>
With use of action attribute. Set it how you need. Or change it in onSubmit form handler.

How do you get user inpup and display it in php

I am trying to get user input from a text box and then echo it using php. Here is my code, and it is not seeming to work.
<html>
<body>
<?php
echo $_POST['value'];
?>
<form method="post" action="">
<input type="text" name="value">
<input type="submit">
</form>
</body>
</html>
<html>
<body>
<?php
if(!empty($_POST['value']))
{
echo filter_var($_POST['value'], FILTER_SANITIZE_STRING);}
?>
<form method="post" action="">
<input type="text" name="value">
<input type="submit">
</form>
</body>
</html>
First check if form posted.

PHP - form action calling itself, how to display everything in 1 page?

I have two .php files as such:
test1.php
<html>
<head>
<title>Sample</title>
</head>
<body>
<form action="test2.php" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
</body>
</html>
test2.php
<html>
<head>
<title>Sample Display Page</title>
</head>
<body>
<?php
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
?>
</body>
</html>
I want to know how can I get it all to display on a single page, i.e. on the test1.php, without having to have two files.
Check if $_POST["userNumber"] isset, and echo the form if it's not.
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
if(isset($_POST["userNumber"]){
echo "You have chosen ".$_POST["userNumber"];
}else{
echo '<form action="test1.php" method="post">';
echo 'Please enter a number <input type="number" name="userNumber"><br>';
echo '<input type="submit">';
echo '</form>';
}
?>
</body>
</html>
Your test1.php will need to look like this
<html>
<head>
<title>Sample</title>
</head>
<body>
<form action="test1.php" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
<?php
if(isset($_POST["userNumber"])) {
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
}
?>
</body>
</html>
You simply need to remove the action="test2.php" section and combine both pages into one just like the following:
<html>
<head>
<title>Sample</title>
</head>
<body>
<form method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
<?php
if(isset($_POST['userNumber'])){
$user_number = $_POST['userNumber'];
echo "You have chosen $user_number";
}
?>
</body>
</html>
Pretty simple actually:
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
if(isset($_POST) && isset($_POST['userNumber'])) {
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
}
?>
<form method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
</body>
</html>
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
if (isset($_POST['submitted'])) {
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
}
else {
?>
<form method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit" name="submitted">
</form>
<?php
}
?>
</body>
</html>
Just use isset() for submit button, note that i added name="submit" to your button since it was missing
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
if(isset($_POST['submit'])
{
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
} else {
?>
<form action="test2.php" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit" name="submit">
</form>
<?php } ?>
</body>
</html>
This one is fully tested and allows you to revise the value after submission. You still have to worry about cross-site scripting attacks, etc... but that is outside of the scope of the question.
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
$user_number = '';
if (isset($_POST['userNumber'])) {
$user_number=$_POST['userNumber'];
?>
<p>You have chosen <?php echo $user_number ?></p>
<?php
}
?>
<form method="post">
Please enter a number <input type="number" name="userNumber" value="<?php echo $user_number ?>"><br>
<input type="submit">
</form>
</body>
</html>
set the variables up before the form as placeholders and check for isset
<?php
$user_number = '';
if(isset($_POST["userNumber"])) {
$user_number = $_POST["userNumber"];
}
?>
<form action="test2.php" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit" name="submit">
</form>
<p>You have chosen: <?php echo $user_number ?></p>
Sample
<form action="<?php $_PHP_SELF ?>" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
</body> </html>

how to pass php variable value to action attribute of html form

I want to pass php variable value as a action to html form. I am trying as follows, but it is not working.
<?php
$url='test.php';
?>
<html>
<body>
<form name="upload" action="<?=$url?>" method="post" >
<input type="submit" value="submit">
</form>
</body>
</html>
All this code are in one php file.
Have you tried <?php echo $url ?>
If it works, then short_open_tag in the php.ini is turned off. That means you will need to either turn it on or use the long open tag <?php throughout your code.
Sounds like you need to enable short_open_tag if your example doesn't work.
<?php
ini_set('short_open_tag', 'on');
$url='test.php';
?>
<html>
<body>
<form name="upload" action="<?=$url?>" method="post" >
<input type="submit" value="submit">
</form>
</body>
</html>
Alternately, write it like this:
<?php
$url='test.php';
?>
<html>
<body>
<form name="upload" action="<?php echo $url ?>" method="post" >
<input type="submit" value="submit">
</form>
</body>
</html>
Try this
<form name="upload" action="<? echo $url ?>" method="post" >
Remove your single quotes:
<form name="upload" action="<?=$url?>" method="post">

Categories