I want to get Multiple Inputs and use them later when Press Submit Button
<!DOCTYPE html>
<html>
<head>
<title>Class Details</title>
</head>
<body>
<?php
$noOfSections=0;
$sectionNameArray= array();
$sectioerr="";
if($_SERVER["REQUEST_METHOD"]=="POST"){
if(!empty($_POST["noOfSections"]))
$noOfSections = $_POST["noOfSections"];
else
$sectioerr="Must Enter Number of Sections";
}
?>
<form action="classdetails.php" method="POST">
<p>Enter Number of Sections : </p>
<input type="text" name="noOfSections">*<?php echo $sectioerr?>
<input type="submit" name="submit" value="Enter"><br>
</form>
<form action="classdetails.php" method="POST">
//Thats the real area about which I am Asking Questions
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){//it is USED to prosses THE STATEMETS BELOW only when the above Submit Button is pressed
echo "<p>Enter Names of Sections : </p><br>";
for($x=0;$x<$noOfSections;$x++){
echo "Enter Name of Section No. ".($x+1)."<br>";
echo "<input type=\"text\">*";
}
echo "<input type=\"submit\" name=\"submitnames\"><br>";
}
?>
</form>
</body>
</html>
I want to Get Multiple Inputs when I press Submit Button Having name attribute "submitnames"
Try adding [] to the end of your dynamic input names
echo "<input type=\"text\" name=\"names[]"\>*";
Edit :Thanks to Magnus Eriksson for pointing out my mistake in the comments.
Related
I have a code which has a form that inputs surface area. db_connect.php connects the database. I am trying to populate a drop down list with a condition that all values that have surface area greater than the value typed into the text field will be displayed in the text field. But when I try to run the code, i'm getting all the values. How can I solve this? Thank you in advance!
<html>
<head>
<title>hi</title>
</head>
<body>
<form>
<p> surface area : <input name = "sa" type = "text"> </p>
<br>
</form>
<select name="areas">
<?php
$sa = $_POST['sa'];
include "db_connect.php";
$displayArea = "SELECT area FROM details where area > '".$sa."'" ;
$sql = mysqli_query($link, $displayArea);
echo "<option> Select </option>";
while ($row = mysqli_fetch_assoc($sql))
{
echo "<option value=\"areas\">" . $row['area'] . "</option>";
}
?>
</select>
</body>
</html>
first you need a submit button into the form.
<input type="submit" value="Submit">
Then if you are using POST you have to specify it as a Form method:
<form method="post">
Then add:
$sa = $_POST['sa'];
echo("[".$sa."]");
to see if "sa" is populated.
If you add a value and click on "Submit" you will see the result.
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.
First, thanks for taking a look at this. I am trying to create an array of forms that acts as a dynamically sized results list. From the results that were given the user can click 'detail' (a submit button) to get further information on the result which is why I am attempting to create an array of forms. Here is what I had tried, which compiled but the buttons aren't doing anything. Any help would be great :)
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<?PHP
$numbers=array(1,2,3,4,5);
$listsize=count($numbers);
for($currentnum=0;$currentnum <$listsize;$currentnum ++){
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<input type="button" value="Submit" name="button<?PHP echo $currentnum?>" />
</form>
<?PHP
echo "<br/>";
}
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
?>
</body>
</html>
This is really just meant to demonstrate what I am trying to do (thought that would be easier without functions out of scope of question).
Try changing the HTML for the button:
<input type="button" value="Submit" name="button<?PHP echo $currentnum?>" />
Should be:
<input type="submit" value="Submit" name="button<?PHP echo $currentnum?>" />
Close bracket for your for loop so that your check is inside it:
<?PHP
echo "<br/>";
}
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
?>
Should be:
<?php
echo "<br/>";
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
}
?>
If you learn to indent your code you'll find these kinds of bugs much easier to spot!
Other than that you're good to go...
You need to change your input types from 'button' to 'submit' so that they submit the forms, then you need to move
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
inside of the for loop
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.
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";
}