PHP Submit button doesn't have any effect (PhpStorm) - php

I updated the question.
Since the last code was pretty complex and even after fixing the stuff it didn't work, I executed the below simple code to check if things work. Even this code doesn't work. Whenever I click on the submit button, it again returns a 404 error.
Yes, I placed the PHP code in the body as well to check if this work but it doesn't.
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<html>
<head>
<title>Echo results!</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
</body>
</html>

Try giving the button_create as name of the submit button

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
if(isset($_POST['button_create'])) {
<td><input type="submit" name="button_create" id="button_create" value="Create Table!"></td>
change these lines see how you go from there

There are a couple of things wrong here, method should be POST instead of GET. The name attribute of text fields should be used when receiving the values. The submit button name should be used to check whether the button is clicked or not. See the example given below.
<?php
if (isset($_POST['submit'])) {
$ex1 = $_POST['ex1'];
$ex2 = $_POST['ex2'];
echo $ex1 . " " . $ex2;
}
?>
<form action="" method="post">
Ex1 value: <input name="ex1" type="text" />
Ex2 value: <input name="ex2" type="text" />
<input name="submit" type="submit" />
</form>

Echo results!
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
this is for your updated question

Related

PHP: HTML Form disappears after submit

I've got a simple <form> in myFunction, as you can see here:
<?php
function myFunction()
{
echo "
<form method='post'>
<fieldset>
<legend>Something</legend>
<input type='text'>
<input type='submit' value='send' name='postform'>
</fieldset>
</form>
";
if (isset($_POST['postform']))
{
echo "I'm working!";
}
}
?>
When I call this function, I can see the form but, when I submit it (by clicking the submit button), it disappears. How can I solve this problem?
Here is the full code:
<?php
echo "
<form method='post'>
<button name='first'>First step</button>
</form>
</div>
";
if (isset($_POST['first']))
{
myFunction();
}
?>
<?php
function myFunction()
{
echo "
<form method='post'>
<fieldset>
<legend>Something</legend>
<input type='text'>
<input type='submit' value='send' name='postform'>
</fieldset>
</form>
";
if (isset($_POST['postform']))
{
echo "I'm working!";
}
}
?>
The issue is that $_POST superglobal is purged on new request, just like when you're navigating through pages, which is quite natural.
So, if one comes to a page with, let's say, $_POST = ['first' => ''], and then he submits a post form (or any form) ['postform' => 'send'], the resulting $_POST would be ['postform' => 'send'].
So, in your case the easiest solution would be either to follow Shailesh's answer or submit the first form with method='get' and, of course, then you'll have to change $_POST['first'] to $_GET['first'].
But a better solution would be to pass some 'step' parameter in request on each step, so you'll have <input type="hidden" name="step" value="1">. And then, depending on a step variable, do some stuff.
Also check out $_SESSION.
Cheers!
It disappears because you don't get to call the function myFunction() itself. The second form does not include the field "first".
If you want it to work "as is", include this in myFunction() code:
function myFunction()
{
echo "
<form method='post'>
<fieldset>
<legend>Something</legend>
<input type='text'>
<input type='hidden' value='send' name='first'>
<input type='submit' value='send' name='postform'>
</fieldset>
</form>
";
if (isset($_POST['postform']))
{
echo "I'm working!";
}
}
?>
The only change is
<input type='hidden' value='send' name='first'>
which makes the form visible again. Anyway, you should rethink this whole code.
if (isset($_POST['first']))
{
myFunction();
}
Replace with:
if (isset($_POST['first']) || isset($_POST['postform']) )
{
myFunction();
}
Move your postform form processing out of the myFunction function definition and call myFunction function from there. Here's the complete code,
<html>
<head>
<title>Page Title</title>
</head>
<body>
<?php
echo "
<form method='post'>
<button name='first'>First step</button>
</form>
";
if (isset($_POST['first'])){
myFunction();
}
?>
<?php
function myFunction(){
echo "
<form method='post'>
<fieldset>
<legend>Something</legend>
<input type='text'>
<input type='submit' value='send' name='postform'>
</fieldset>
</form>
";
}
if (isset($_POST['postform'])){
myFunction();
echo "I'm working!";
}
?>
</body>
</html>
write the same as two different documents.
<html>
<body>
<form method="post" action="submit.php">
<fieldset>
<legend>Something</legend>
<input type="text" name="input">
<input type='submit' value='send' name="postform">
</fieldset>
</form>
now write the following in submit.php
<?php
$input=$_POST['input'];
if ($input!="")
{
echo "I'm working!";
}
?>
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "User Has submitted the form and entered this name : <b> $name </b>";
echo "<br>You can use the following form again to enter a new name.";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
Change this place on this code:
<?php
echo "
<form method='post'>
<button name='first'>First step</button>
</form>
</div>
";
if (isset($_POST['first']) or isset($_POST['postform']))
{
myFunction();
}
?>

need to do a query when button is pressed php

I want to so when a button is pressed on my page it runs a query in the database
View
<form method="POST" action="evcccontroller/query">
<input type="submit" name="nw_update" value="NW_Update"/>
</form>
Controller
public function query()
{
// load the index view
$this->load->view('querycheck');
}
querycheck.php
<?php
if(isset($_POST['nw_update']))
{
echo("You clicked button one!");
//and then execute a sql query here
}
else
{
echo" dhur";
}
An Error Was Encountered
The action you have requested is not allowed.
I think problem with your action attr of form try like
<form method="POST" action="<?php echo base_url(); ?>/evcccontroller/query">
<input type="submit" name="nw_update" value="NW_Update"/>
</form>
add base url to form action
action="<?php echo base_url(); ?>/evcccontroller/query"
Just try
<form method="POST" action="<?php echo site_url('evcccontroller/query')?>">
<input type="submit" name="nw_update" value="NW_Update"/>
</form>
code is looking fine. I think the form action url is wrong. try this:
<form method="POST" action="<?=base_url('index.php/evcccontroller/query')?>">
<input type="submit" name="nw_update" value="NW_Update"/>
</form>

Why does it always echo the "Not set!" though I enter data?

Though I enter data and hit Submit it always echoes the else part always. I know it isn't the type of question to be asked on Stackoverflow but...
<html>
<head>
<title>Sticky Form</title>
</head>
<body>
<form method="POST" action=<?php echo $_SERVER['PHP_SELF'] ?>>
<label for="Name">Name</label>
<input type="text" name="FName">
<input type="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$f_name = $_POST['FName'];
echo "$f_name";
}
else
{
echo "Not set!";
}
?>
</body>
</html>
Change this:
<input type="submit">
to
<input type="submit" name="submit">
P.S: key name in global arrays comes from users input ($POST,$_GET,$_COOKIE), if you want to change its key, you need to change that element's name!

Cannot submit HTML form using PHP on iPad

Recently I got this problem that I cannot solve. My HTML form cannot be submitted by using iPad, but the same form CAN be submitted from my laptop (windows machine). Please check this out:
http://www.gomap.ch/admin/pages/test.php - this is submitted correctly
http://ipadpreview.com/previewer?url=www.gomap.ch/admin/pages/test.php - this is not submitted.
This is a very simple form, and code goes like this:
<?php
error_reporting(0);
if($_POST['mirko']){
echo 'hello, Mirko';
}
?>
<form action="" method="POST">
<input type="submit" value="Submit" name="mirko"/>
</form>
Any help, please??
Thanks a lot!
You are not using valid html.Replace your code with this and have u tried on real ipad.
<html>
<head></head>
<body>
<?php
error_reporting(0);
if($_POST['mirko']){
echo 'hello, Mirko';
}
?>
<form action="" method="POST">
<input type="submit" value="Submit" name="mirko"/>
</form>
</body>
</html>
You need to add the name of the form which holds the php code into the action. In this case it would be the same form you're on. Follow what I did below with nameOfFile.php being the name of the file you posted.
<?php
error_reporting(0);
if($_POST['mirko']){
echo 'hello, Mirko';
}
?>
<form action="nameOfFile.php" method="POST">
<input type="submit" value="Submit" name="mirko"/>
</form>

submiting form and then executing php code

On basis of the information that the user fills in my form, I want to execute some PHP code. So after the form is submitted, it should have control on the same page and thereafter it should execute the PHP code (and not before pressing submit button). I have used <input type="hidden" value=1 name="hid"/>. When the user clicks the submit button, the value is changed to 0. But its not working. so solution please..
Is this similar to what you are looking for ?
<?php
if (!isset($_POST["submit"]) ) {
if ($_POST["hid"] == 0 ) {
echo "hid is not 0. display form.";
}
?>
<html>
<head>
<script type="text/javascript">
function check_valid() {
document.getElementById("hid").value = 0;
}
</script>
</head>
<body>
<form method="POST" action="<?php echo $PHP_SELF;?>" onsubmit="return check_valid();" >
<input type="hidden" id="hid" name="hid" value="1" />
<input type="submit" value="submit" name="submit"/>
<!-- form elements go here -->
</form>
</body>
</html>
<?php
} else {
echo "hid is now 0, execute the php code";
}
?>
EDIT: added <input type="hidden" name="hid" value="1" /> for clarity. Thanks to andre_roesti for the suggestion

Categories