No action happens when form button pressed - php

I have a form that contains two button
but when I press on one of them nothing happen
and I can't find the problem can you help me please !!
here my php code :
<?php
session_start();
include 'connection.php';
// in this section, I retrieve data from database and display them on table
// if agree button pressed do the following
if (isset($_POST['agree']))
{
$que="update project set status='submitted' ,projectstatus=1 where projectid=$id ";
$result3=mysql_query($que);
if ($result3)
{
echo(" <script>
alert('The project has been approved');
</script>");
header( "Location:unsubmited.php" );
}
else
{
echo "an error occur can't agree on this project";
}
}
?>
and this is the form :
<form action='' method='post'>
<input type='button' name='disagree' value='disagree ' class='styled-button-11'>
<input type='button' name='agree' value='agree' class='styled-button-11'>
</form>
thanx ^^

Change your code for this, as it says andrewsi:
<form action='' method='post'>
<input type='submit' name='disagree' value='disagree ' class='styled-button-11'>
<input type='submit' name='agree' value='agree' class='styled-button-11'>
</form>

I believe this is as simple as, filling in the action='' to the page to post back to and setting the type='submit' as andrewsi suggested.

Related

Browser go back button Confirm Form Resubmission

I have a search form on my page.
<form action='' method='post'>
<input type='text' name='search' />
<input type='sumit' name='submit' value='submit'/>
</form>
When the user clicks the submit button on the form, it should run a mysql_query and create a link to the user page.
if(isset($_POST['search'])){
$add = "city = {'$_POST['search']'}";
}
$res = mysql_query("SELECT * FROM user WHERE {$add}");
while($rw=mysql_fetch_object($res)){
echo "<a href=user.php?id={$rw->user_id}?>{$rw->name}</a>";
}
When I click on the link user.php?id=3, it goes to the user page and everything is OK. But I have problem when I click the browser's back-button, on user.php page. Then i have problem back to previous page.
Confirm Form Resubmission.
Correct the following:
<input type='sumit' name='submit' value='submit'/>
You are writing type='sumit' instead of type='submit'
To not display the "Confirm Form Resubmission" alert, do you have to change your submission (method) to GET.
<form action='' method='GET'>
<input type='text' name='search' />
<input type='sumit' name='submit' value='submit'/>
</form>
...
if(isset($_GET['search'])){
$res = mysql_query(sprintf("SELECT * FROM user WHERE city='%s'", mysql_real_escape_string($_GET['search']) ));
while($rw=mysql_fetch_object($res)){
echo "<a href=user.php?id={$rw->user_id}?>{$rw->name}</a>";
}
}
You should use get method instead of post. Post resubmissions must be confirmed by most browsers.
P.s.: you shouldn't pass the user input into your sql query directly to prevent the risk of sql injections.

How to loop forms in one page based on the number of entries

i am doing a crime management web application using php.
The page is like this when run it,it gives a chance to enter Details about the case,then click on submit button it brings another form on the same page.
that Form will ask you to enter the number of accused involved,if lets say for instance you have two accused,it will bring another form on the same page that will allow you to enter personal data bout accused number one ...then click on submit button it bring you another form that asks you to enter the number of crimes accused number one has committed...if lets say again he committed 3 crimes then it brings a select form which allows you to select the crime and submit 3 times.
After the third entry it will go back again and give you a form to enter personal details about accused number two....same process as accused one an when you are done it takes you back to the initial point where it would be ready to enter a new case
i pray its clear....please help
this is my earlier code ...ready to run
<?php
session_start();
if(isset($_SESSION["start"]))
{
switch($_SESSION["now"])
{
case 0:
$_SESSION["now"]++;
echo " <p style='text-align:center;'><H1> ==Welcom Page, you need to start here==</h1></p>
<br><br><br><br>
<form action='index.php' method='post' name='page'>
<p h3>Please Enter your max number....
<input type='number' name='max' style='width:140px;' required='required'/>
<input type='submit' name='GO'></p>
</form>";
break;
case 1:
if(isset($_POST[max]))
{
$_SESSION["end"]=$_POST[max];
echo "<p style='text-align:center;'><H2> Sequence 1</h1></p>
<form action='index.php' method='post' name='page'>
value 1: <input type='text' name='x' style='width:140px;'/><br>
value 2: <input type='text' name='y' style='width:140px;'/><br>
value 3: <input type='text' name='z' style='width:140px;'/><br>
<p style='text-align:center'><input type='submit' name='Next'>
</form>";
$_SESSION["now"]++;
}
else
{
if($_SESSION["now"]==1)
{
$_SESSION["now"]=0;
header( "Location: index.php");
exit;
}
}
break;
default:
if($_SESSION["now"]<=$_SESSION["end"])
{
$x=$_SESSION["now"];
$_SESSION["now"]++;
echo "<p style='text-align:center;'><H2> Sequence $x</h1></p>
<form action='index.php' method='post' name='page'>
value 1: <input type='text' name='x' style='width:140px;'/><br>
value 2: <input type='text' name='y' style='width:140px;'/><br>
value 3: <input type='text' name='z' style='width:140px;'/><br>
<p style='text-align:center'><input type='submit' name='Next'>
</form>";
}
else
{
echo "<p style='text-align:center;'><H2> Successful, Please refresh the page to restart your sequence ...</h1></p>";
session_destroy();
}
}
}
else
{
$_SESSION["start"]=true;
$_SESSION["now"]=0;
header( "Location: index.php");
exit;
}
?>

this.form.submit() with PHP isset?

echo "<form method=\"post\" action=\"settings.php\" onchange=\"this.form.submit()\">";
echo "Announce New Files: <input type=\"radio\" name=\"announcefiles\" value=\"On\" $checkon1> On";
echo "<input type=\"radio\" name=\"announcefiles\" value=\"Off\" $checkoff1> Off<br>";
echo "</form>";
I am trying to get this form to submit when one of the radio buttons is pressed but I'm not sure how to catch the submission.
for example, normally with a submit button you would use something along the lines of if(isset($_POST['submit'])) but I'm not sure how to do it if the form auto submits.
Add hidden input field like:
<input type="hidden" name="action" value="submit" />
Then in PHP check:
if(isset($_POST["action"]) && $_POST["action"] == "submit") { ... }
You should be checking the request method. If you've set things up cleanly, a POST request at that URL will mean a form submit. As you've noticed, you can have attempted submits where a value just isn't there.
if ($_SERVER['REQUEST_METHOD'] === 'POST')
See $_POST vs. $_SERVER['REQUEST_METHOD'] == 'POST' for more discussion.
Give your form a name and check for isset($_POST['form_name']) or check for the name of the radio isset($_POST['announcefiles'])
Also, you don't need all the quote escaping that you have, you can use single quotes as well as use a multiline string - see example below.
echo "
<form method='post' name='form_name' action='settings.php' onchange='this.form.submit()'>
Announce New Files: <input type='radio' name='announcefiles' value='On' $checkon1> On
<input type='radio' name='announcefiles' value='Off' $checkoff1> Off<br>
</form>";
<?php
// Check if form was submitted
if (isset($_POST['form_name']) {
// Form submitted
}
?>
<?php
// Check if radio was selected
if (isset($_POST['announcefiles']) {
// Form submitted
echo 'You chose' . $_POST['announcefiles'];
}
?>
Try this:
You may have an easier time if you separate the php and html a little more.
<form method="post" action="settings.php" onchange="this.form.submit()">
<fieldset>
<legend>Announce New Files:</legend>
<label for="on"><input type="radio" id="on" name="announcefiles" value="On" <?php echo $checkon1 ?> /> On</label>
<label for="off"><input type="radio" id="off" name="announcefiles" value="Off" <?php echo $checkoff1 ?> /> Off</label>
</fieldset>
</form>
Then in your php logic in settings.php ( or above the form if you are posting back to the same page ) you can check for the value of announcefiles:
<?php
if(isset($_POST['announcefiles'])){
// DO SOMETHING
}
?>
Let me know if this helps. Or if I'm missing the question.

PHP $_POST Alert

I have this form which allows the input of any product quantity from 1-10:
<form method='post' action='cart.php'>
<input type='number' name='quantitychange' size='2' min='1' max='10' value=".$_SESSION["itemsSelected"][$i][1].">
<input type='hidden' name='ProductID' value=".$_SESSION["itemsSelected"][$i][0].">
<input type='submit' value='Update'>
</form>
And another form (button) to display a selection of payment modes:
<form action='cart.php' method='post'>
<input type='hidden' name='next'>
<input type='submit' value='Select Payment Mode'>
</form>
What I want to happen is that when a user did not input anything (1st form), ex. null or 0, I want to display an alert box that says 'Product quantity can't be null or 0'.
Here's my code for that:
if (isset($_POST['next'])) {
if ($_POST['quantitychange']==null || $_POST['quantitychange']==0) {
?>
<script type='text/javascript'>
alert('Product quantity can't be null or 0.');
</script>
<?php
}
else {
echo "
//Payment modes here
";
}
}
The error is that even when a user inputs a quantity bet. 1 to 10, it still displays the alert message. Any help? Thank you.
By the way, the input type "number" only works in Google Chrome browser.
Use a small javascript (or jQuery) function to validate the form before posting it. Have this function throw up the alert if your condition isn't met and then return false. If the condition is met, return true, and it gets submitted.
Edited to add since this might get googled, I'll help a bit with code snippet I have used. The below example is jQuery and was used in production for a web application I made for my employees. document.form.doit.submit(); should be the pure javascript way of submitting the form.
<script type="text/javascript">
function subForm() {
// document.form.doit.submit();
if( test condition passes ) {
$('#save_order').submit();
}
}
</script>
<form id="save_order" action="oms_db.php" method="POST">
<input id="doit" type="button"
value="i am a button" onClick="subForm();">
</form>
I think you have some error in your forms. Instead of the below:
<input type='number' name='quantitychange' size='2' min='1' max='10' value=".$_SESSION["itemsSelected"][$i][1].">
<input type='hidden' name='ProductID' value=".$_SESSION["itemsSelected"][$i][0].">
you should be using something like this:
<input type='number' name='quantitychange' size='2' min='1' max='10' value="<?php echo $_SESSION["itemsSelected"][$i][1]; ?>">
<input type='hidden' name='ProductID' value="<?php echo $_SESSION["itemsSelected"][$i][0]; ?>">
The value parameters in the hidden input fields needs to be echoed from PHP. What you have now is like the value is simple strings ".$_SESSION["itemsSelected"][$i][0].".
I suggest you use
if(empty($_POST['quantitychange'])) { echo 'yourerror'; }
As it is far cleaner then your script. (http://php.net/manual/en/function.empty.php)
Update:
Also, you can't use two seperate forms like you do, your browser only posts whats between
<form>
</form>
Using only one will fix your problem.

post an array to webpage

it has error because $_POST['sub1'] can't be accessed
is there any approach or solution to echo the value of $_POST['sub1']? or impossible? no way? even with another arrays?
i had question about my code nobody solved it! then I decide to tell it in simple way.
<html>
<form method="post">
<input type='submit' name='sub1' value='sub1'>
<?php
if(array_key_exists('sub1',$_POST))
{
echo"<input type='submit' name='sub2' value='sub2'>";
}
if(array_key_exists('sub2',$_POST))
{
echo $_POST['sub1'];
}
?>
</form>
</html>
I think I know what is wrong here.
When you submit the form the second time (for sub2) you are no longer posting the value of sub1 along with it, just sub2.
This should fix it:
<html>
<form method="post">
<input type='submit' name='sub1' value='sub1'>
<?php
if(array_key_exists('sub1',$_POST))
{
echo"<input type='hidden' name='sub1' value='" . htmlentities($_POST['sub1']) . "'>";
echo"<input type='submit' name='sub2' value='sub2'>";
}
if(array_key_exists('sub2',$_POST))
{
echo $_POST['sub1'];
}
?>
</form>
</html>
You're using submit buttons. Only the button that you actually click will have its name/value pair sent to the server. When you click the sub2 button, only sub2=sub2 is sent, so sub1 won't exist in the $_POST array.
followup:
$_POST is created for you by PHP based on what's sent from the browser. The way you've built your form makes it impossible for 'sub1' to exist when you click the 'sub2' button. In other words, you need to use the SAME name= for BOTH buttons, and change the value= as appropriate:
html:
<input type="submit" name="submit" value="sub1" />
<input type="submit" name="submit" value="sub2" />
php:
if (isset($_POST['submit'])) {
echo "You clicked the {$_POST['submit']} button";
}

Categories