Firstly here is my PHP CODE
if(!isset($_POST['selection'])){
$missing['selection'] = $required['selection'];
}
if(empty($missing)) {
post2session();
$_SESSION['step'][0] = 0;
redirect("");
}
Here is my HTML
<form action="" method="post">
<table cellpadding="0" cellspacing="0" border="0" class="tbl_insert">
<tr>
<th><label for="selection">Select from following that applies to you</label></th>
<td>
<input type="radio" name="selection" id="selection" group="form_type" value="form1"> />Form 1<br />
<input type="radio" name="selection" id="selection" group="form_type" value="form2" />Form 2<br />
<input type="radio" name="selection" id="selection" group="form_type" value="form3" />Form 3<br />
<input type="radio" name="selection" id="selection" group="form_type" value="form4" />Form 4<br />
</td>
</tr>
</table>
</form>
How would i redirect the user to FORM1 if they selected radio "form1"; FORM2 if they selected "form2"; etc.
I appreciate the help you will provide (Y)
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
switch($_POST['selection']) {
case 'form1': $url = '/wherever/form1/is'; break;
case 'form2': $url = '/wherever/this/other/form/goes'; break;
...
default: $url = '/some/default/handler';
}
redirect($url);
}
Redirects to new pages are normally done like this:
header('Location: www.site.tld/anotherpage.php');
exit();
first turn name attribute into array name="selection[]"dio
and also do not use same id for each radio button, either u r able to check all radio button together,because php works with name and javascript works wth id
and on post page
try to check via print_r($_POST['name'])
Related
I have a problem with my form. I need it to redirect user to different pages basing on which radio button was selected. User has to choose one of two options and click next, and according to his choice, page should redirect him to other page.
Here is the code as it looks for now
<fieldset>
<legend>Select option</legend>
<center>
<form method="post" action="">
<input type="radio" name="radio1" value="Osoba fizyczna"/>Non-company
</br>
<input type="radio" name="radio2" value="Firma"/>Company
</br>
<input type = "submit", class = "buttonStyle2", value=""/>
</form>
</center>
</fieldset>
and then php code
if(isset($_POST['Company'])
header("Location: http://myaddress.com/company.php");
Big thanks in advance for your help
Here is one way to achieve this.
Sidenote: Make sure you're not outputting before header. Consult this page on Stack about possible Headers already sent..., should this occur and making sure error reporting is set/on.
Otherwise, PHP will fail silently.
if(isset($_POST['radio1']) && ($_POST['radio1']) == "Osoba fizyczna"){
header("Location: http://www.example.com/non_company.php");
}
elseif(isset($_POST['radio1']) && ($_POST['radio1']) == "Firma"){
header("Location: http://www.example.com/company.php");
}
else{
header("Location: http://www.example.com/redirect_to_home.php");
}
Nota: The else would be if the person did not make a choice and simply clicked on submit without making a selection.
while using radio buttons of the same group name in your form:
<input type="radio" name="radio1" value="Osoba fizyczna"/>Non-company
</br>
<input type="radio" name="radio1" value="Firma"/>Company
Note about
<input type = "submit", class = "buttonStyle2", value=""/>
remove the commas
<input type = "submit" class = "buttonStyle2" value=""/>
Since HTML source in FF will reveal No space between attributes in red/as an error.
<fieldset>
<legend>Select option</legend>
<center>
<form method="post" action="">
<input type="radio" name="radio1" value="Osoba fizyczna"/>Non-company
</br>
<input type="radio" name="radio1" value="Firma"/>Company
</br>
<input type = "submit" class = "buttonStyle2" value=""/>
</form>
</center>
</fieldset>
and
if ( isset($_POST['radio1']) ) {
$filename = $_POST['radio1'] . "php";
header("Location: http://myaddress.com/".$filename);
}
Might be even better to set up an array with allowed values and check if radio1 is in that array.
I am trying to validate the values of a radio button group in php.
The radio buttons are dynamically created in the form.
I can validate the radio button if it is only one radio group, for example.
<form>
<input type="radio" name="radio1">
<input type="radio" name="radio1">
</form>
this is for passing values of radio button
if(isset($_POST['radio1']))
{
*some codes
}
Since the radio buttons in my form are dynamically created, the names of the radio groups increment like radio1, radio2, radio3 so on.
How can I make validation for this dynamic radio button group?
Better create radio button with name as array. Like
<form method="post">
<input type="radio" name="radio[1]">
<input type="radio" name="radio[2]">
</form>
and server side you can check with a foreach
foreach($_POST['radio'] as $key=>$radio){
if($radio == "on"){
echo "$key is checked";
}
}
Try this
<form>
<input type="radio" class="rdo" name="radio[]">
<input type="radio" class="rdo" name="radio[]">
.....
<input type="radio" class="rdo" name="radio[]">//n value
</form>
var arr = new Array();
$('.rdo:checked').each(function() {
arr.push($(this).val());
});
In server side
$i=0;
if(count($_POST['radio'])==0){
return false;
}
foreach($_POST['radio'] AS $rs){
if($rs!=''){
//Some code
}
else{
$i++;
}
}
if($i==count($_POST['radio'])){
return false;
}else{
//some code
}
You can try using radio elements as array like below:
<form method="post">
<input type="radio" name="radio[0]" value="0.1">
<input type="radio" name="radio[0]" value="0.2">
<input type="radio" name="radio[1]" value="1.1">
<input type="radio" name="radio[1]" value="1.2">
<input type="submit" name="s" value="Submit" />
</form>
and from server try
if(isset($_POST['radio'])){
echo "<pr>";
print_r($_POST);
}
I am creating a form in php on the page "index.php". When the page is loaded the following form is created:
if($_SERVER['REQUEST_METHOD']!='POST')
{
echo '
<form action="index.php" method="POST">
<fieldset style="width: 700px;">
<legend>Enter your search below</legend>
<textarea rows="1" cols="80" name="query">
</textarea>
</fieldset>
<p>
<input type="radio" value="Non-Aggregated"> Non-Aggregated
<input type="radio" value="Aggregated"> Aggregated
<input type="submit" value="Search">
</p>
</form>';
}
When the user clicks the submit button, the appropriate content is displayed:
else
{
if ($_POST['query'])
{
//content displayed after form submission
}
}
Going back to the form, note the radio options:
<input type="radio" value="Non-Aggregated"> Non-Aggregated
<input type="radio" value="Aggregated"> Aggregated
Is there a condition that I can place in the if-statement to carry out a different action based on whether Non-Aggregated or Aggregated is selected from the radio buttons; and if so how would I go about doing this?
Thanks for any help at all.
Give name attribute to radio buttons, like
<input type="radio" name="aggr" value="Non-Aggregated"> Non-Aggregated
<input type="radio" name="aggr" value="Aggregated"> Aggregated
After POST method is executed you can check values with PHP:
if($_POST['aggr']=='Aggregated'){
//DO STUFF
}
if($_POST['aggr']=='Non-Aggregated'){
//DO OTHER STUFF
}
In other way, you can set names, like
<input type="radio" name="Non-Aggregated" value="Non-Aggregated"> Non-Aggregated
<input type="radio" name="Aggregated" value="Aggregated"> Aggregated
And check this if isset
if(isset($_POST['Aggregated'])){
//DO STUFF
}
if(isset($_POST['Non-Aggregated'])){
//DO OTHER STUFF
}
First of all, asign a "name" to your radios. Same to both.
<input type="radio" name="aggregation" value="Non-Aggregated"> Non-Aggregated
<input type="radio" name="aggregation" value="Aggregated"> Aggregated
On the "else" clause, make a small change, insted of
if($_SERVER['REQUEST_METHOD']!='POST')
use
if($_SERVER['REQUEST_METHOD']=='POST')
And last, but not least, add your condition inside that last if...
if ($_POST['aggregation'] == 'Aggregated')
{
// Actions for Aggregated
}
else
{
// Actions for Non-Aggregated
}
Here is working code:
<?php if($_SERVER['REQUEST_METHOD']!='POST')
{
$form = <<<FORM
<form action="" method="POST">
<fieldset style="width: 700px;">
<legend>Enter your search below</legend>
<textarea rows="1" cols="80" name="query">
</textarea>
</fieldset>
<p>
<input type="radio" value="Non-Aggregated" name='radioCheck'> Non-Aggregated
<input type="radio" value="Aggregated" name='radioCheck'> Aggregated
<input type="submit" value="Search">
</p>
</form>
FORM;
echo $form;
}
elseif($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['radioCheck']) && $_POST['radioCheck'] =='Non-Aggregated'){
// Non-Aggregated form
echo 'Non-Aggregated';
}elseif($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['radioCheck']) && $_POST['radioCheck'] =='Aggregated'){
// Aggregated form
echo 'Aggregated';
}
?>
<?php
if($_SERVER['REQUEST_METHOD']!='POST')
{
// echo the form and don't forget to give the radio inputs a name
// <input name="agg" type="radio" value="Non-Aggregated"> Non-Aggregated
// <input name="agg" type="radio" value="Aggregated"> Aggregated
}
else if (isset($_POST['agg']))
{
if ($_POST['agg'] == 'Aggregated') {
// do one thing
} else {
// do something else
}
}
Is this better then?
<form id="form1" name="form1" method="post" action="cart.php">
<input name="size" type="radio" value="Small">Small<br>
<input name="size" type="radio" value="Large">Large<br>
<input name="size" type="radio" value="XXL">XXL<br>
<input type="hidden" name="sizes" id="sizes" value=""/>
<input type="hidden" name="pid" id="pid" value="85" />
<input type="submit" name="button" value="Add To Cart"/></form>
Just a quick note that for retreiving the result I am using this code here:
if(isset($_POST['sizes'])){
$myvar = $_POST['sizes'];
echo "Your Size:", $myvar ;
}
You seem to be calling the radio buttons 'form1' but you have the same name for the first hidden field.
I would suggest that the form is submitting this field and ignoring the radio buttons for this reason.
Looking at your revised code, surely you need:
$myvar = $_POST['size'];
if ($myvar) {
echo "Size is $myvar";
}
'sizes' is never getting set to a 'true' value.
Note: the if ($myvar) works, in this instance, as effectively as the 'isset' statement but is simpler to code and read.
With JavaScript & jQuery:
$("#form1 input[name='size']").click(function(){
var size = $('input:radio[name=size]:checked').val();
$("#form1 input[name='sizes']").val(size);
});
That should do the trick.
I need to get textbox value to another php page. By clicking 'Box' icon i want to submit perticular row only. I already got row ID to 'Box' icon. How can i do that with the while loop.
thanks in advance
Tharindu
You should arrange the HTML code for this in the following fashion:
<table>
<form method="post">
<tr>
<td>Z0678<input type="hidden" name="id" value="Z0678"></td><td><input type="text" value="0" name="qty"></td><td><input type="image" src="box.gif"></td>
</tr>
</form>
<form method="post">
<tr>
<td>Z0678<input type="hidden" name="id" value="Z0678"></td><td><input type="text" value="0" name="qty"></td><td><input type="image" src="box.gif"></td>
</tr>
</form>
<form method="post">
<tr>
<td>Z0678<input type="hidden" name="id" value="Z0678"></td><td><input type="text" value="0" name="qty"></td><td><input type="image" src="box.gif"></td>
</tr>
</form>
<form method="post">
<tr>
<td>Z0678<input type="hidden" name="id" value="Z0678"></td><td><input type="text" value="0" name="qty"></td><td><input type="image" src="box.gif"></td>
</tr>
</form>
</table>
Then once you hit the image, it will submit the form. Add this code to the top of your PHP script "<?php print_r($_POST); ?> and you will see that you can now process the posted contents based on the data that was posted without the need for any while loop.
let the you have posted be list.php
in the text box like
<input type=text name="rid" value= " <?php echo $rid ?>" onclick="location.href='view.php'"/>
get the row id in next page that is view.php
$id = $_GET['rid'];
pass it as hidden in view.php
<input type="hidden" name="id" value="<?php echo $id; ?> "/>
Make sure all your db connection goes perfect and echo all data whatever you want from row.
In the original php file generate a different html form for each box.
<form action="page2.php" method="post">
<input name="Z067DA" />
</form>
In the page2.php file use code similar to this. $value contains the user
submitted information.
foreach($_POST as $key=>$value)
{
// Use submitted value.
}
If you know the input tag names in advance you can just access them directly in your php code.
$value = $_POST['Z067DA'];