I want to echo something once a form is submitted. But when I click the submit button, it seems that the page is just refreshing itself and I do not see the word that I have written in the echo section. Here is my code:
<?php
if (isset($_POST['submit'])) {
echo "submitted";
}
?>
<h3>Post your form here</h3>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<label>Insert a title here</label><br>
<input name="title" type="text" placeholder="add a title"><br><br>
<label>Insert the body here</label><br>
<textarea name="body" placeholder="insert the body here "></textarea><br><br>
<input type="submit" value="submit" name="submit"><br>
</form>
I also tried the code by removing the isset function, but that did not work, either.
In form action, you have missed an echo.
action="<?php echo $_SERVER['PHP_SELF']; ?>"
This is working code. I test it on my Machine.
Why you don't use else statement to test it better.
here is the code.
Note (Check your localhost server settings)
<?php
if (isset($_POST['submit'])) {
echo "submitted";
}
else
{
echo "Not working";
}
?>
<h3>Post your form here</h3>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<label>Insert a title here</label><br>
<input name="title" type="text" placeholder="add a title"><br><br>
<label>Insert the body here</label><br>
<textarea name="body" placeholder="insert the body here "></textarea><br>
<br>
<input type="submit" value="submit" name="submit"><br>
</form>
Actually the credit goes to #ashok. He was right that I needed to check localhost server settings.
I am using Phpstorm to write PHP codes. Whenever I click on the Chrome browser to see the results, it takes me to
http://localhost:63342/ name of the file page.
The port 63342 is the default port used by Phpstorm. Since I am using Xampp and it runs on port 8080. I changed port number 63342 to 8080, and it worked.
If you are submitting to the same page, you really dont need to define an action.
Instead of isset, use !empty as such
if(!empty($_POST['submit'])){
echo "success";
}
I must bring to your attention that if the suggestions don't work, you should start with debugging what you are actually receiving from the form POST, using:
<pre>
<?php var_dump($_POST); ?>
</pre>
Turning on error reporting in your php settings is also a good start for debugging.
Related
I am trying to make simple login system. But I encountered a problem with post method. Simple register.php file is not working:
<?php
if (isset($_POST['username'])) {
print('text');
}
?>
<html>
<body>
<form method="post">
<input type="text" name="username" required/>
<button type="submit" name="submit">Register</button>
</form>
</body>
</html>
After pressing a button it opens 404 error page.
I searched through dozens of similar questions but still got no solution. I'm working on localhost, PhpStorm 2016.3.2 and PHP7.0 and Ubuntu 16.04
SOLUTION:
It was built-in PhpStorm server's fault. It's not working when post method is used.
I'm using now Lampp and post works fine.
It's work. Are you start Xampp Server or Lampp Severr on your Ubuntu ? I think you forgot to start Server
Try:
<?php
if (isset($_POST['submit'])) {
$username = $_POST['username'];
echo $username;
}
?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="username" required />
<button type="submit" name="submit">Register</button>
</form>
</body>
</html>
Give the action attribute to the form tag and see.. Here I have added index.php.. U can change it to the same file u r working on.. This might solve the problem.
<form action="index.php" method="post">
<input type="text" name="username" required/>
<button type="submit" name="submit">Register</button>
</form>
I've just finished setting up AMP on a new MacOS El Capitan installation. I don't know why but every time I try to run a simple HTML form with some PHP processing I get a 404 error. I've tried it with the default MacOS document root, I've also tried with the user-level document root, and I've even installed MAMP as a last-resort move. In all these setups the result is always the same when I hit the submit button: 404.
<?php if(isset($_POST['submit'])) {
$name = $_POST['name'];
$post = $_POST['post'];
$link = $_POST['link'];
echo $name;
echo $post;
echo $link;
echo "all good";
}
?>
<form name="upload" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
Name:<br>
<input type="text" name="name"><br>
Post:<br>
<input type="text" name="post"><br>
Link:<br>
<input type="text" name="link"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
The error is "The requested URL/< was not found on this server."
What am I missing??
Many thanks in advance for your help!
Looks like the action for your form isn't parsing correctly. Your code runs fine on my server as it is - however, every server is different.
You could try moving the double quotes inside the php section to see if that's tripping up the parser somehow:
Change
<form name="upload" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
to
<form name="upload" action=<?php echo "\"".htmlspecialchars($_SERVER['PHP_SELF'])."\""; ?> method="post">
or alternatively
<form name="upload" action=<?php echo "'".htmlspecialchars($_SERVER['PHP_SELF'])."'"; ?> method="post">
In theory all of these should result in the same thing - but it's worth a try.
After submitting data from a form, I want to be redirected to a page, here's my code :
<form action="#result" method="POST">
<input name="zipcode" type="text" placeholder="Your ZipCode" />
<input name="zipcode_submit" type="submit" value="Send" />
</form>
<div id="result">
<?php
if(isset($_POST['zipcode_submit'])) {
header("Location: http://twitter.com");
}
?>
</div>
It does not work for me and I don't know why
Thanks for your help
try to shift the php code, above the form tag,i.e
<?php
if(isset($_POST['zipcode_submit'])) {
header("Location: http://twitter.com");
}
?>
above
<form action="#result" method="POST">
Have a look at php.net docs regarding the header function. It has to be the first output to the website. Put your form at the end of the file.
<html>
<?php
/* this will produce an error, header must be the first output on the website */
header('Location: http://www.example.com/');
exit;
?>
You should place a exit; after the header statement, to fully prevent the following code from being executed.
It is very difficult for me to put in words my query. But I will try.
I have a site xyz.com which has search facility for listed products. The search page url is generated like this :www.wyz.com/search/search_term
I want to create a iframe page in a third party site with a search facility which can directly communicated with my site xyz.com.
I have tried to create a search box with a submit button. I want to append the search query in as a variable to my form action url string.
So the search string should look like this :www.wyz.com/search/my_string_variable
The code I have written is:
<?php
$url='http://www.xyz.com/search/';
?>
<?php
if (isset($_POST['submit']))
{
$r1=$_POST['num1'];
}
?>
<?php
$result=$url.$r1
?>
<html><body>
<form action="<?php echo $result; ?>" method="post">
Num1:<input name="num1"><br>
<input type="submit" name="submit">
</form>
</body></html>
==================================================================
But output what I get, is only "http://www.xyz.com/search/". It removes my variable from the url. I am not able to find what is the reason? I have also tried to print result via to check the actual output and it shows that it has added the value at the end of url. But when I want to achieve the same thing via form action it does not work. please help?
<?php
$url='http://www.xyz.com/search/';
?>
<?php
if (isset($_POST['submit']))
{
$r1=$_POST['num1'];
$result=$url.$r1;
header("location:$result");
}
?>
<html><body>
<form action="" method="post">
Num1:<input name="num1"><br>
<input type="submit" name="submit">
</form>
</body></html>
Please try the above code. I have made some modifications. The main reason your code is not working is whenever you press the submit button it is going to the the url "http://www.xyz.com/search/" directly .The if condition is never executed. In the above mentioned code it will work properly
action="" - you are submitting to the wrong url. Here is alternate version -
<?php $url='http://www.xyz.com/search/';
if (isset($_POST['submit'])) {
$r1=$_POST['num1']; header("Location: ".$r1); // 302 redirection
}
?>
<html><body> <form target="_SELF" method="post"> Num1:<input name="num1" type="text" /><br /> <input type="submit" name="submit" /> </form> </body></html>
I have a simple form for a mailing list that I found at http://www.notonebit.com/projects/mailing-list/
The problem is when I click submit all I want it to do is display a message under the current form saying "Thanks for subscribing" without any redirect. Instead, it directs me to a completely new page.
<form method="POST" action="mlml/process.php">
<input type="text" name="address" id="email" maxlength="30" size="23">
<input type="submit" value="" id="submit"name="submit" >
</form>
You will need AJAX to post the data to your server. The best solution is to implement the regular posting, so that will at least work. Then, you can hook into that using Javascript. That way, posting will work (with a refresh) when someone doesn't have Javascript.
If found a good article on posting forms with AJAX using JQuery .
In addition, you can choose to post the data to the same url. The JQuery library will add the HTTP_X_REQUESTED_WITH header, of which you can check the value in your server side script. That will allow you to post to the same url but return a different value (entire page, or just a specific response, depending on being an AJAX request or not).
So you can actually get the url from your form and won't need to code it in your Javascript too. That allows you to write a more maintanable script, and may even lead to a generic form handling method that you can reuse for all forms you want to post using Ajax.
Quite simple with jQuery:
<form id="mail_subscribe">
<input type="text" name="address" id="email" maxlength="30" size="23">
<input type="hidden" name="action" value="subscribe" />
<input type="submit" value="" id="submit"name="submit" >
</form>
<p style="display: none;" id="notification">Thank You!</p>
<script>
$('#mail_subscribe').submit(function() {
var post_data = $('#mail_subscribe').serialize();
$.post('mlml/process.php', post_data, function(data) {
$('#notification').show();
});
});
</script>
and in your process.php:
<?php
if(isset($_POST['action'])) {
switch($_POST['action']) {
case 'subscribe' :
$email_address = $_POST['address'];
//do some db stuff...
//if you echo out something, it will be available in the data-argument of the
//ajax-post-callback-function and can be displayed on the html-site
break;
}
}
?>
It redirects to a different page because of your action attribute.
Try:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="text" name="address" id="email" maxlength="30" size="23" />
<input type="submit" value="" id="submit" name="submit" />
</form>
<?php if (isset($_POST['submit'])) : ?>
<p>Thank you for subscribing!</p>
<?php endif; ?>
The page will show your "Thank You" message after the user clicks your submit button.
Also, since I don't know the name of the page your code is on, I inserted a superglobal variable that will insert the the filename of the currently executing script, relative to the document root. So, this page will submit to itself.
You have to use AJAX. But that requires JavaScript to be active at the users Brwoser.
In my opinion it's the only way to do without redirect.
to send a form request without redirecting is impossible in php but there is a way you can work around it.
<form method="post" action="http://yoururl.com/recv.php" target="_self">
<input type="text" name="somedata" id="somedata" />
<input type="submit" name="submit" value="Submit!" />
</form>
then for the php page its sending to have it do something but DO NOT echo back a result, instead simply redirect using
header( 'Location: http://yourotherurl.com/formpage' );
if you want it to send back a success message simply do
$success = "true";
header( 'Location: http://yourotherurl.com/formpage?success='.$success);
and on the formpage add
$success = $_GET['success'];
if($success == "true"){ echo 'Your success message'; } else { echo
'Your failure message';
Return and print the contents of another page on the current page.
index.php
<html>
<body>
<p>index.php</p>
<form name="form1" method="post" action="">
Name: <input type="text" name="search">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$_POST['search'];
include 'test.php';
}
?>
</body>
</html>
test.php
<?php
echo 'test.php <br/>';
echo 'data posted is: ' . $_POST['search'];
?>
Result:
Just an idea that might work for you assuming you have no control over the page you are posting to:
Create your own "proxy php target" for action and then reply with the message you want. The data that was posted to your php file can then be forwarded with http_post_data (Perform POST request with pre-encoded data). You might need to parse it a bit.
ENGLISH Version
It seems that no one has solved this problem without javascript or ajax
You can also do the following.
Save a php file with the functions and then send them to the index of your page
Example
INDEX.PHP
<div>
<?php include 'tools/edit.php';?>
<form method="post">
<input type="submit" name="disable" value="Disable" />
<input type="submit" name="enable" value="Enable" />
</form>
</div>
Tools.php (It can be any name, note that it is kept in a folder lame tools)
<?php
if(isset($_POST['enable'])) {
echo "Enable";
} else {
}
if(isset($_POST['disable'])) {
echo "Disable";
} else {
}
?>
Use
form onsubmit="takeActions();return false;"
function takeAction(){
var value1 = document.getElementById('name').innerHTML;
// make an AJAX call and send all the values to it
// Once , you are done with AJAX, time to say Thanks :)
document.getElementById('reqDiv').innerHTML = "Thank You for subscribing";
}