PHP submit button not working. - php

<?php
if(isset($_POST['submit'])){
header('Location: http://www.rate.ee');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>
</title>
</head>
<body>
<input type="submit" name="submit" id="submit" class="button" value="Submit"/>
</body>
</html>
This is my code. Very simple, isn't it. But it doesn't work and I don't get it.. I always thought that PHP only executes when I load the page, but the other page where I use same code works very well without JS..

You need to wrap your button in a <form> tag:
<form action="" method="post">
<input type="submit" name="submit" id="submit" class="button" value="Submit"/>
</form>

You need a form surrounding the input.
<body>
<form action="welcome.php" method="post">
<input type="submit" name="submit" id="submit" class="button" value="Submit"/>
</form>
</body>
</html>

You have no form tag and even if you did it would need the method="post" attribute.
<form method="post" action="<?php echo $_SERVER[PHP_SELF]?>">
<input type="submit" name="submit" id="submit" class="button" value="Submit"/>
</form>

Related

Using if(isset($_POST['submit'])) to display message but it is not working

I tried to write simple Form validation with PHP. When click submit button if the name is empty then the website will ask the user to fill in. But when i click the submit button nothing appear on the screen. Can you help me how to fix this problem? Thank you. Here is my code :
<?php
if (isset($_POST["submit"])){
if(!empty($_POST["Ename"])){
}else{
echo"Please enter your name";
}
}
?>
<html>
<form class="" action="index.php" method="post">
Tutor name: <input type="text" name="Ename" value="">
<input type="button" class="submit" value="Submit your record" name="submit">
</form>
</html>
write <input> tag in <form> tag and set input type="submit"
<?php
if (isset($_POST["submit"])) {
if (!empty($_POST["Ename"])) {
} else {
echo "Please enter your name";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<form action="" method="POST">
Tutor name: <input type="text" name="Ename" value="">
<input type="submit" class="submit" value="Submit your record" name="submit">
</form>
</body>
</html>
You need to wrap your form inputs and submit button in a <form> tag, like this:
<html>
<form method="post">
Tutor name: <input type="text" name="Ename" value="">
<input type="button" class="submit" value="Submit your record" name="submit">
</form>
</html>

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"];?>

Change web page contents before it gets displayed to the user using php

buttons.php
<!DOCTYPE html>
<head>
<title>
Button Page
</title>
</head>
<body>
<button name="btn1">Button 1</button>
<button name="btn2">Button 2</button>
<button name="btn2">Button 3</button>
</body>
</html>
and the result.php
<!DOCTYPE html>
<head>
<title>
Result Page
</title>
</head>
<body>
<p class="parag1">This value is for Button1</p>
<p class="parag2">This value is for Button1</p>
<p class="parag3">This value is for Button1</p>
</body>
</html>
what I want to happen is when the user clicks a button, the paragraphs in the result.php will change to values assigned for that button before it gets displayed to the user. Thanks!
First off I would recommend looking into PHP POST and GET methods, they will give you a more complete picture of how form data can be passed from one page to another.
Regarding your question all you would need to do here is wrap each button in a form like so:
<form action="results.php" method="POST">
<button name="btn1">Button 2</button>
</form>
<form action="results.php"method="POST">
<button name="btn2">Button 2</button>
</form>
<form action="results.php" method="POST">
<button name="btn3">Button 3</button>
</form>
I would recommend using a hidden value that is readonly and changing your button to a submit, this will allow you to hide the values you would like to pass across and stop unwanted editing of the values by the users.
e.g.
<form action="results.php" method="POST">
<input readonly type="hidden" name="buttonValueOne" value="Button 1"/>
<input type="submit" name="btn1">Button 1/>
</form>
<form action="results.php"method="POST">
<input readonly type="hidden" name="buttonValueTwo" value="Button 2"/>
<input type="submit" name="btn2">Button 2/>
</form>
<form action="results.php" method="POST">
<input readonly type="hidden" name="buttonValueThree" value="Button 3"/>
<input type="submit" name="btn3">Button 3/>
</form>
Then on the results page, you would use if statements to check which one was passed over:
if (isset($_POST["buttonValueOne"]) { ?>
<p class="parag1"><?php echo $_POST['buttonValueOne']; ?></p>
<?php
}
?>

How to call a submit handler in PHP?

I want to call my function when I click on my submit button.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form name="bfe-form" method="POST" action="index.php">
<label>Input Url</label>
<input name="url" type="text" value="">
<input name="submit" type="button" value="Submit">
</form>
</body>
</html>
<?php
var_dump($_POST);
var_dump($_GET);
if (isset($_POST['submit'])) {
getContent();
}
function getContent() {
$content = file_get_contents($_POST['url']);
return $content;
}
?>
But I get nothing my php just won't be executed.
Your Submit button should be type=submit instead of button.
it will look like this!
<input name="submit" type="submit" value="Submit">
I'm assuming this page is index.php
<input name="submit" type="submit" value="Submit">
Instead of use type button use type submit

How to post a hidden form value to a PHP page

<!doctype html>
<html>
<head>
<body>
<form action="question.php" method="post">
<input type="hidden" name="session" value="12" />
</form>
</body>
</html>
<?php
header('Location: question.php');
?>
question.php File...
<?php
$rr = $_POST['session'];
echo $rr;
?>
it should print 12 right?
But i get this error "Notice: Undefined index: session in C:\wamp\www\Project\question.php on line 3".
what is the problem here?
thanks...
This will echo your value once you click the submit button.
question.php File
<?php
$rr = $_POST['session'];
echo $rr;
?>
HTML current page
<!doctype html>
<html>
<head>
<body>
<form id="myForm" action="question.php" method="post">
<input type="hidden" name="session" value="12" />
<input type="submit" value="submit" />
</form>
</body>
</html>
// this will auto submit your form using javaScript
<script type="text/javascript">
document.getElementById("myForm").submit();
</script>
But what if i want to submit the from automatically without a submit button:
jsfiddle
<form action="question.php" method="post">
<input type="hidden" name="session" value="12" />
<input type="submit" value="submit" id="sub"/>
</form>
jQuery:
$(function(){
$('#sub').click();
});
wow, pretty complicated... all you needed to do was add
<?php
session_start();
// session_start has to be the first line
// of your php and before any HTML code
?>
<html>
<body>
</body>
</html>
<!doctype html>
<html>
<head>
<body>
<form action="question.php" method="post">
<input type="hidden" name="session" value="12" />
<input type="submit" name="send" value="send">
</form>
</body>
</html>
question.php:
<?php
# prints all post elements
print_r($_POST); // for method="post"
echo $_POST['session']; # only the hidden input named "session"

Categories