HTML form POST method not working (despite URL params showing) - php

Got a form setup to submit some stuff.
There are three submit buttons, all with the same name (choice) and different IDs (1, 2 & 3).
Using POST method to submit the form to form.php
Form.php loads and I can see the form params in the URL.
However there is no POST data coming in.
Index.php:
<form action='form.php' action='POST'>
<input type='hidden' name='index' value='".$cell_count."'>
<div class='btn-group btn-group-m'>
<button name='choice' value='1' type='submit' class='btn btn-default btn-danger'>
...
</button>
<button name='choice' value='2' type='submit' class='btn btn-default btn-warning'>
...
</button>
<button name='choice' value='3' type='submit' class='btn btn-default btn-success'>
...
</button>
</div>
<button name='choice' type='submit' value='4' class='btn btn-default btn-sm'>
...
</button>
Form.php :
<?php
var_dump($_POST);
if($_POST['choice'] == 3) {
echo "Chose 3";
}
else if($_POST['choice'] == 2) {
echo "Chose 2";
}
else if($_POST['choice'] == 1) {
echo "Chose 1";
}
echo "index: " . $_POST['index'];
?>
Result :
http://i.stack.imgur.com/JrB7j.png
Thanks for any help you can offer!

You are mixing the attributes:
<form action='form.php' action='POST'>
Should be:
<form action='form.php' method='POST'>
Now you don't have a method attribute, so the form is sent by GET (the default).

You're form should be using method="POST". The fact that you are seeing the parameters means that your form is submitting as GET.
The form code should be:
<form action='form.php' method='POST'>

The action isn't supposed to be post, the METHOD is post.
<form action='form.php' method='post'>

Related

How to pass the userID value after clicking Submit button php

I'm facing some error when i click submit button, the value for userID is not passed. below is my code.
<div> <input type='hidden' id='userID' value="<?$inputs ['userID'] ?? '' ?>" >
</div>
<button type='submit' name="update">Update</button>
after clicking Update button, the info will be passed to function. But the value passed for the userID is NULL. Can someone assist me?
If you have wrapped your form by form tag you should have name attribute to send your data
<form action="destination.php">
<div> <input type='hidden' name='userId' id='userID' value="<?$inputs ['userID'] ?? '' ?>" >
<button type='submit' name="update">Update</button>
</form>

Show a button via PHP

I want to show the second button when i click on the first button , I've coded this but for an unknown reason it didn't work . hope anyone help me:
<input class="btn btn-success" type="submit" name="bb1" value="Show Button" "></input>
<a href="#">
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<?php
if(isset($_POST['bb1'])){
echo "<input class=\"btn btn-success\" type=\"file\" name=\"bb1\" value=\"Show ME\" \"></input>
";?>
The problem has been solved, i just need to add the input inside the form.

Click event on two buttons for if else condition is not working

Advance thanks for your help. I am not understanding why it is not working. I am a beginner and hence I am not finding the problem. Please anyone help me...
echo "<form method='POST'><br>
<p>Would you Like to create your Account with Details Information<b>?
</b>
<br><br>
<input type='button' id='yes' name='yes' value='Yes'/>
<input type='button' id='no' name='no' value='No'/></p>
</form>";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['yes'])) {
echo "Yes Clicked";
} else if (isset($_POST['no'])) {
echo "<br><br><p> You have successfully Registered!!!</p>";
}
}
Replace button with submit
<input type='submit' id='yes' name='yes' value='Yes'/>
<input type='submit' id='no' name='no' value='No'/></p>
try this code :
<?php
echo "<form method='POST' action='index.php'><br>
<p>Would you Like to create your Account with Details Information<b>?
</b>
<br><br>
<input type='submit' id='yes' name='yes' value='yes'/></p>
<input type='submit' id='no' name='no' value='No'/></p>
</form>";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['yes'])) {
echo "<br><br><p> Yes clicked!!!</p>";
} else if (isset($_POST['no'])) {
echo "<br><br><p> You have successfully Registered!!!</p>";
}
}
?>
form are trigged with input type submit , so if you want to get value of two input , they must be a submit input instead of button . in this cas , you have two input type submit , theey should be type submit with different name .that's all
You have to change your button's type attributes to submit instead of button, or else they won't actually submit the form.
<input type='submit' id='yes' name='yes' value='Yes'/>

Saving info while submitting page with GET in PHP

<form method='get' action='y.php'>
<div>
<input type='text' id='txtName' name='txtName'/>
<input type='submit' value='submit' id='submit'/>
</div>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
if (isset($_GET['btnSave'])) {
$name=isset(($_GET['txtName'])?isset($_GET['txtName']:'');
//then Logic of insert goes here
}
}
?>
so before moving to y.php the record must be saved.
but I cant get the $name value, as action given to y.php.
How can I get $name which contain value in text box.
if you change the action to this (same/current) page record is going to database without any flaw or error.
try using post method instead and change your code accordingly, try this:
<form method='post' action=''>
<div>
<input type='text' id='txtName' name='txtName'/>
<input type='submit' value='submit' id='submit' name='submit'/>
</div>
</form>
<?php
if (isset($_POST['submit'])) {
$name=$_POST['txtName'];
//then Logic of insert goes here
//redirect to y.php with name value
echo "<script>window.open('y.php?user=$name','_self')</script>";
}
?>
Then use $nme = $_GET['user']; to get the value of $name in y.php
Try this code,
<form method='get' action=''>
<div>
<input type='text' id='txtName' name='txtName'/>
<input type='submit' value='submit' id='submit'/>
</div>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
if (isset($_GET['txtName'])) {
$name=$_GET['txtName'];
//then Logic of insert goes here
}
}
?>
I suggest you use method post. You can code like this
<form method='post' action=''>
<div>
<input type='text' id='txtName' name='txtName'/>
<input type='submit' value='submit' id='submit'/>
</div>
</form>
<?php
if(isset($_POST['txtName'])){
$name =$_POST['txtName'];
echo $name;
}
?>
you can use the session if you want make the value use in another file.
I hope that can solve your problem

PHP Like Button Problems

I am trying to make a like button for posts on my website.
PHP for like query (d_db_update is
function d_db_update($string) {
return mysql_query($string);
}
)
if($_GET['like']) {
$like = d_db_update("UPDATE posts set post_rating = post_rating+1 WHERE post_id = {$_GET['like']}");
}
Button
<form action='{$_SERVER['PHP_SELF']}&like={$posts_row['post_id']}' method='get'>
<p align='left'>{$posts_row['post_rating']}
<input type='submit' name='like' value='Like' /></p>
</form>
What can I do to fix it/make it work?
Use below form with a hidden input it solve your problem.
<form action='{$_SERVER['PHP_SELF']}' method='get'>
<p align='left'>{$posts_row['post_rating']}
<input type='hidden' name='like' value='{$posts_row['post_id']}' />
<input type='submit' value='Like' /></p>
</form>
You are using your form action wrong.. if you are using get method than there is not need to use the form..
try this..
<a href='yourpage.php?like=<?php echo $post_id ?>'>Like</a>
your submit button name and like variable which you have used in action url are the same , and you used get method in method of form.So, you need to change the submit button name.
or
you can do it without using form only on button click try below code
<input type='button' name='like' value='Like' onclick="location.href='yourpage.php?like=<?php echo $post_id ?>'" />
Change your code to this
You can not write PHP variables using {}. You need to echo them out.
<form action='' method='get'>
<p align='left'><?php echo $posts_row['post_rating'] ?>
<input type='hidden' name='like' value='<?php echo $posts_row["post_id"] ?>' />
<input type='submit' value='Like' /></p>
</form>
Edit--
You were not returning the post id correctly, I made the changes, also there is no need to provide any action as it will be self only.

Categories