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";
}
Related
I have the following code which is causing me problem. The code generates two buttons, one to remove the selected project and the other to cancel the deletion of the project. The remove function works well, however I haven't found a way to make my cancel button redirect to my url (when I click on it, nothing happens). Any clue?
...
echo "<form method='post'>";
echo "<input type='hidden' value='".$currentid."' name='project'/>";
echo "<b>".$project_name."</b>";
?>
<div class="btn_2"> <input type="submit" name="save" value="Remove"><input type="submit" onclick="window.location.replace('www.myurl.ca')" value="Cancel"></div>
</form>
You're using a submit button and tagging JS on to it. I'm expecting that you're actually submitting the form, but I could be wrong.
If nothing is happening with your JS, you may try doing window.location.href = "www.myurl.ca"; return false; where including the return false should avoid the submit.
The best solution would probably be to make it a plain button and wrap it in an A tag. You would avoid submitting your form, redirect, and all without requiring JS be active.
...
echo "<form method='post'>";
echo "<input type='hidden' value='".$currentid."' name='project'/>";
echo "<b>".$project_name."</b>";
?>
<div class="btn_2"> <input type="submit" name="save" value="Remove"><button type="button">Cancel</button></div>
</form>
Using the formaction button attribute works.
<input type="submit" name="save" value="Cancel" formaction="http://www.myurl.ca">
I have made a few radio buttons in my php code
echo "<input type='radio' name='rad' value='a'>";
echo "<input type='radio' name='rad' value='b'>";
and my html code is
<form method="post" action="fetch_page_4.php">
<input type="submit" name="submit1" value="Positive" id="btn1"/>
<input type="submit" name="submit2" value="Negative" id="btn2"/></form>
Whenever i click the Positive or Negative button it should go into the respective part in PHP...which it does. But the problem is that i need to find out which of my radio buttons is checked and take the action accordingly.
But i am unable to do so. My code for it is
if(isset($_POST['submit1']))
{
$main_key=0;
if(isset($_POST['rad']) && ($_POST['rad'])=='a')
{
$sub_arr=explode(" ",$array_subject[0]);
$count_sub=count($sub_arr);
$body_arr=explode(" ",$array_message[0]);
$count_msg=count($body_arr);
$main_key=1;
}
}
If i echo the value of "main_key" after this...it is coming out to be 0, thus indicating that it is not going inside the loop even when the radio button is checked and has the required value.
Can somebody please tell me why is this happening?
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.
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.
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.