I am very new to PHP and I am trying to display the value of a selected radio button in a string. But all I get returned is "on" instead of the value associated with the selection. Any help would be great!
<form action="Handle_form_PHP.php" method="post">
<div class= "row">
<div class ="col-25">
<label for="student">Student</label>
</div>
<div class ="col-75">
<input type="radio" name="student" value ="student">
</div>
</div>
<div class= "row">
<div class ="col-25">
<label for="student">Employee</label>
</div>
<div class ="col-75">
<input type="radio" name="student" value = "employee">
</div>
</div>
<div class="row">
<input type="submit" value="Submit">
</div>
</form>
This is the PHP I am using to print:
// Create a shorthand for the form data:
$fname = $_REQUEST['firstname'];
$lname = $_REQUEST['lastname'];
$address = $_REQUEST['address'];
$state1 = $_REQUEST['state1'];
$state2 = $_REQUEST['state2'];
$student = $_POST['student'];
// Print the submitted information:
echo "<p>Thank you, <b> $student </b>, <b>$fname $lname</b>, for the filling out our survey:<br>
<p>We will Send you your report at: <i>$address</i>, <i>$state1</i><i>$state2</i>.</p>\n";
If your $_POST['student'] superglobal variable was holding a value of 'on', then the issue has nothing to do with method case-sensitivity.
The default value for a radio button is 'on' unless you provide a value attribute inside the radio button tag.
Let me prove it to you. Go to [cringe] https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_form_checkbox then paste the following snippet in the left-side box, then Run, then Submit.
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php" method="get">
<input type="radio" name="student" checked> Student<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
This is submitting with a method of get (notice the lowercase get is inconsequential. post will behave the same way). See that without a value in the student radio input, the submitted value is on.
If you write value="Red42" inside the radio input, you will no longer receive on; the submitted value is Red42.
use POST not post when saying method:
<form action="Handle_form_PHP.php" method="POST">
not
<form action="Handle_form_PHP.php" method="post">
For the record, the book was incorrect and had "post"
Related
I have reviewed and tried the answers already given on this forum but nothing worked. So now I am sharing my issue here. I want selected radio button value from view to controller to do further action on the basis of selected radio button. But its not working, receiving blank result. My view and controller code is below
<form>
<div class="inline">
<input class="magic-radio" type="radio" name="conn_ty" value="12m" id="12m" checked />
<label for="12m">
12 month terms
</label>
</div>
<div class="inline">
<input class="magic-radio" type="radio" name="conn_ty" value="24m" id="24m"/>
<label for="24m">
24 month terms
</label>
</div>
<div class="inline hidden">
<input class="magic-radio" type="radio" name="conn_ty" value="36m" id="36m"/>
<label for="36m">
36 month terms
</label>
</div>
</form>
Contoller:
$connection_term = $this->input->post('conn_ty');
echo $connection_term;
from your view:
Use echo form_open($url); instead of form tag and create a submit button.
On your controller
$connection_term = $this->input->post('conn_ty');
echo $connection_term;
put this on your view
echo form_open('on controller which function u want to get value');
and you have to declare
$RADIO_VALUE = array('name' => 'radio button name','id' => 'radio button id');
don't forget
echo form_close();
Have this form:
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST" id="form_pesquisa" data-toggle="validator" role="form">
<div class="radio"> <label> <input type="radio" name="cliente" id="73" value="ss"> "ss" </label> </div>
<div class="radio"> <label> <input type="radio" name="cliente" id="74" value="aa"> "aa" </label> </div>
<button type="submit" class="btn btn-warning" name="hchoose">Choose client</button>
How can I get the id of the radio when the button is pressed? $_POST['id'] does not work. Tks all.
The id doesn't get passes to the server for processing, only the input name and value get passed. PHP uses name to process the user input & $_POST['anything'] is used to get the user input of <input type="radio" name="anything" id="73"> & not the id="73". JavaScript can be used to get the id but not PHP actually.
If you really want to get whatever is in the id you can put it in value like value="ssId-73"
You have to set name for the button eg. <input type="radio" name="myRadioButton"/> then in PHP $_POST['myRadioButton']
There's no way to receive id in PHP, you can only set same name as id.
If you don't want to edit your form (which will be the easiest way), you'll have to use jQuery, because using native Javascript will make it even more difficult.
Below your form:
<script src="jQuery_url">
</script>
<script>
jQuery("#form_pesquisa").on("submit", function() {
var id = jQuery("input[name='cliente']:checked").attr("id");
jQuery(this).append("<input type='hidden' name='id' value='"+id+"'>");
});
</script>
Having done this, in your form handler file, $_POST['id'] will contain the value you want to catch.
This is probably a really stupid question, but I can't figure out why my code is doing this. I'm trying to do a simple recordset update and yet when I hit the submit button on the page, it will reload the page and in the url have multiple url parameters of the info from the form.
I have two forms in the whole document, one, the one I'm working with is supposed to change a variable in the database to the username of the user
<form action="<?php echo $editFormAction; ?>" method="POST" name="Catch">
<input name="Horseid" type="hidden" value="<?php echo $colname_WildHorse?>"/>
<input name="Owner" type="hidden" value="<?php echo $colname_HorseImage?>"/>
<input name="Catch" type="submit" value="Catch"/>
<input type="hidden" name="MM_update" value="Catch"/>
</form>
This is the one that returns all the url stuff after I hit the submit button. The second form is under it, and I want that one to display in the Url, which it does perfectly fine. Why is this one freaking out though?
URL produced:
http://localhost:8888/TheMeadow.php?Horseid=38&Owner=redlilac78&Catch=Catch&MM_update=Catch
This is the result code: I left out the style sheet since it's a lot
<title>The Meadow</title>
<style type="text/css">
//Left out style
</style>
</head>
<body>
<div id="Background">
<div id="Logo"><img src="Images/RR_Art/Website pages/Logo/Ropin' Ranch logo.gif" width="229" height="192" /></div>
<div id="Map">Map</div>
<div id="Directories">Directories</div>
<div id="LogOut">Log out</div>
<div id="Note">
<form action="" method="get">
<p>
Welcome to the wild, where you can catch and search for wild horses! You can only catch one horse every two days so choose your horse wisely! </p>
<div id="Name"><br> <br> <br> <br> <br>
<form name = "WildHorse" id = "WildHorse" onSubmit="TheMeadow.php?HorseId43">
<input type = "hidden" name = "HorseId" value = "43"/>
<input type = "submit" name = "" value = "Search" />
</form>
</div>
</div>
</body>
In your form action the problem is with $editFormAction in php , it is either empty or having the same files name due to which the form is being submitted to the same page.
Here's the form, found on a webpage named genericwebpage.html
<form name = "quoted" form action = "genericwebpage.php" method="get">
<input id = "poster" type="text" name="poster" placeholder = "Credited Individual."> <br>
<textarea class = "actual-quote" name = "actual-quote"placeholder = "Write the quote here!"></textarea><br><br>
<input id = "submit1" type="submit">
</form>
and heres the php, found on genericwebpage.php
<div class="wrapper">
<div class="submissions">
<div class="logo-logo"><h2>Generic.</h2></div>
<div class="top-submit"><?php echo $_GET['actual-quote']?></div>
<div class="poster"><?php echo $_GET['poster']?></div>
</div>
Can someone guide me to a point where an error box will show up when entered not fully and so it will not be submitted? I've looked at tutorials but nothing seems to be working.
On the inputs you want to be required
required="required"
will stop the submission and point out the required field in most modern browsers. For a more in - depth response you will need JavaScript.
Hopefully I'm making this more difficault then it has to be. Here is what I'm trying to do. I have a form that does a POST and returns data. I have a second form that then asks the user a yes/no question based on data from the first form. Is it possible to capture the POST data from the first form submission and pass it along with the second form POST?
Here is my scenario
if ($_POST['button_1']) {
$params = $_POST;
print_r($_POST);
// process form data
}
if ($_POST['button_2']) {
// Retain the POST data from the first submission
$new_params = $params . $_POST;
print_r($new_params);
// process form data and do some additional stuff
}
<form id="form_1" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
...
<input type="submit" value="Button" name="button_1" id="button_1"/>
</form>
<form id="form_2" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
...
<input type="submit" value="Button" name="button_2" id="button_2"/>
</form>
Is there a way to do this easily or am I over complicating this?
You can either use a hidden field in your second form, or sessions. Start reading here:
http://www.php.net/manual/en/book.session.php
Yes, the standard way is to use type = "hidden" fields to pass this context data onto the second form. There are lots of worked examples to see how to do this. View the source of this HTML page or any other with forms on them and search for "hidden" to see how the applications do this.
There are a few ways to "fake" this.
Have the first form submit to itself and just load the $_REQUEST variables and use them to populate the second form with the appropriate data/options.
have the 2nd form loaded via ajax once the first is submitted and use javascript to grab the current form variables and provide them to the ajax function.
Which method would you prefer?
UPDATE:
This is long, but a working example
<!DOCTYPE html>
<html>
<body>
<form name="first" method="post">
<input type="hidden" name="action" value="firstformdone">
<div style="width:100px;float:left;">
Age:
</div>
<div style="width:200px;float:left;margin-left:20px;">
<input type="text" name="age">
</div>
<div style="clear:both;"></div>
<div style="width:100px;float:left;">
Name:
</div>
<div style="width:200px;float:left;margin-left:20px;">
<input type="text" name="name">
</div>
<div style="clear:both;"></div>
<br>
<?PHP
if (!$_REQUEST['action'] == "firstformdone") {
?>
<input type="submit" value="contine">
<?PHP
}
?>
</form>
<?PHP
if ($_REQUEST['action'] == "firstformdone") {
?>
<form name="second" action="something_else.php" method="post">
<input type="hidden" name="age" value="<?PHP echo $_REQUEST['age']; ?>">
<input type="hidden" name="name" value="<?PHP echo $_REQUEST['name']; ?>">
<div style="width:150px;float:left;">
Preferred games:
</div>
<div style="width:200px;float:left;margin-left:20px;">
<select name="games">
<option value="">Select games</option>
<?PHP
if ($_REQUEST['age'] <= 10) {
?>
<option value="tlddlywinks">Tiddly Winks</option>
<option value="Jacks">Jacks</option>
<option value="Go-Fish">Go-Fish</option>
<option value="Hid-And-Go-Seek">Hid-And-Go-Seek</option>
<?PHP
} else {
?>
<option value="Halo">Halo</option>
<option value="StarWars">The Old Republic</option>
<option value="LaserTag">Laser Tag</option>
<option value="spin-the-bottle">spin-the-bottle</option>
<?PHP
}
?>
</select>
</div>
<div style="clear:both;"></div>
<br>
<input type="submit" value="Next!">
</form>
<?PHP
}
?>
</body>
</html>
You need to package the data from the first form up for resubmission. You can use a hidden fields for that job:
foreach ($_POST as $key => $value) {
print "<input type='hidden' name='".htmlspecialchars($key, ENT_QUOTES, "UTF-8")."' value='".htmlspecialchars($value, ENT_QUOTES, "UTF-8")."'>";
}