I have a situation where i do need unchecked radio button value. Is it possible to send unchecked radio button value through form in php? If yes, then can you please explain?
<input type="hidden" name="myField" value="false" />
<input type="checkbox" name="myField" value="true" />
both fields have the same name, if the checkbox isn't checked, the hidden field will be submitted, if the checkbox is checked, it will virtually override the the hidden field.
On a side note, are your sure radio buttons wouldn't be better suited to your needs, you will be able to see the definite answer for each questions, whereas checkboxes are more for submitted only the selected checkboxes. Failing that you could always assume that all checkboxes that were not submitted as checked, could be assumed to be unchecked.
Create sample.php file and paste below code and run it.
<?php
echo "<pre>";
if(isset($_POST['numbers']) && isset($_POST['unchecked']))
{
$checked_items = $_POST['numbers'];
$unchecked_items = array_diff($_POST['unchecked'],$_POST['numbers']);
echo 'Checked Item<br/>';print_r($checked_items);
echo '<br/><br/>Unchecked Items<br/>';print_r($unchecked_items);
}
?>
<form name='frmname' action='' method='post'>
<input type='radio' name='numbers[]' value='one'/>One
<input type='radio' name='numbers[]' value='two' />Two
<input type='radio' name='numbers[]' value='three' />Three
<input type='hidden' name='unchecked[]' value='one' />
<input type='hidden' name='unchecked[]' value='two' />
<input type='hidden' name='unchecked[]' value='three' />
<input type='submit' name='submit' value='Submit' />
</form>
I have solved it. I have sent both radio button values in hidden field and as well as radio button values. On receiving page, I have compare checked value with hidden field value and so I got unchecked value. Thanks for all who responded to my question.
Related
I want to pass the value from this page to next page pay.php file below is my form (index.php )
<form action=\"/modules/gateways/pay.php\" method=\"post\" id=\"checkout[id]\">
<input type=\"hidden\" name=\"user\" value=\"[userid]\">
<input type=\"hidden\" name=\"decription\" value=\"[itemname]\">
<input type=\"hidden\" name=\"amount\" id=\"amount[id]\" value=\"[price]\">
<input type=\"hidden\" name=\"type\" value=\"deposit\">
</form>
and in pay.php, I am trying to get the value with this code:
$m_amount = number_format($_POST['amount'], 2, ".", "");
$m_desc = strip_tags($_POST['decription']);
I am not getting values in pay.php.
You need to check your HTML.
<form action="/modules/gateways/pay.php" method="post" id="checkout[id]">
<input type="hidden" name="user" value="123">
<input type="hidden" name="decription" value="itemone">
<input type="hidden" name="amount" id="amount_id" value="538">
<input type="hidden" name="type" value="deposit">
</form>
and in pay.php try to receive to simply print POST variable
print_r($_POST);
Include JQuery or a similar JS framework or use native JS to submit this form.
Without a submit-button you have to submit this form by JS or a JS-framework.
In JQuery you have to write something similar to this example:
$('#checkout[FORMID]').submit();
This will submit the form. For sure you have to complete this but basicly this is the missing call.
I saw your form contains hidden inputs. If this form depends on another form/action or else you should either include the hidden form in the depoending form or submit it by itself using JS/JQuery/something similar.
i have a form with Variables coming from mysql query , and i passing from a form 2 value of radio and hidden input , no problem with radio .. but hidden not passing Correct value , passing only first value found it on the page .
I want to get 2 value of radio and hidden together when i Choose the currently selected radio button
<input type="radio" name="radio" value="<?php echo $awardid ; ?>" />
<input type="hidden" name="point" value="<? echo $point ; ?>" />
after print :
<input type="radio" name="radio" value="1">
<input type="hidden" name="point" value="3">
<input type="radio" name="radio" value="2">
<input type="hidden" name="point" value="5">
<input type="radio" name="radio" value="3">
<input type="hidden" name="point" value="8">
elc ...
As an example , when i Choose Second radio , passing Correct value of radio 2 and passing value of hidden 3 -> " that's not Correct value , must be 5 " .
every and any Choose for radio , passing with it first value of hidden on page -> 3
, when i Change hidden input to radio input and Choose it , passing the Correct value without any problem ...
so this problem happen when the input is hidden .. why ? and Solution ?
Radio inputs share a name in order to define the group.
Hidden inputs cannot share names, as they are discreet entities.
I'd suggest appending the $awardid to the hidden input's name.
<input type="hidden" name="point<?php echo $awardid; ?>" value="<?php echo $point ; ?>" />
Then, you can get the value of that particular input based on the selected radio button.
$radio = $_POST['radio'];
$point = $_POST['point'.$radio];
All of your form elements of the same input type share the same name. The name field of the form elements are the keys of the key/value pairs in form submissions. Thus, when you have 3 hidden input fields with the same value for the name field, it must choose one. Remember, the hidden input fields do not correlate at all with the radio input fields which are simply organized close to each other in the code. Your browser does not know that your point fields have anything to do with the radio fields.
You should have unique names for all your input fields, like so
<input type="radio" name="radio" value="1">
<input type="hidden" name="point1" value="3">
<input type="radio" name="radio" value="2">
<input type="hidden" name="point2" value="5">
<input type="radio" name="radio" value="3">
<input type="hidden" name="point3" value="8">
And then you can retreive their values with
$_POST['radio']
$_POST['point1']
and so on.
EDIT: However, that does mean that for every form submission, EVERY hidden field's data will be sent every time, regardless of which radio input is selected. To solve this, you can intercept the form submission on the front end with an event listener such as .submit(), and then disable the input fields you do not want to be submitted prior to allowing the form submission to go through.
you can rename point to point[radio buttons value]
so that when you request point as array, you can get the value from it something like this #$_REQUEST["point"][#$_REQUEST["radio"]];
So as discussed on the comments. I will show my approach.
So as you mentioned that you have a mysql query to create your inputs (point and radio) I think that is something like:
Please disconsider syntaxe as it been a while since I programmed on php
$query = "select radio, point from someTable where someconditions_here ";
while( /*there is some row*/ ){
echo "<input type=\"radio\" name=\"radio\" value=\"".$row[radio]."\">";
echo "<input type=\"hidden\" name=\"point\" value=\"".$row[point]."\">";
}
My suggestion would be you just print the radios and after submit it you check the point associated to it, something like this:
if ( isset($_POST['radio']) ){
$qryToFindPoint = "select point from someTable where sameconditions_here AND radio = " . $_POST['radio'] ;
//do whatever you need here with the selected point
}else{
$query = "select radio, point from someTable where someconditions_here ";
while( /*there is some row*/ ){
echo "<input type=\"radio\" name=\"radio\" value=\"".$row[radio]."\">";
}
}
This is just the idea. Of course you should use proper mysqli or PDO functions to do your queries and avoid sql injection.
With this approach you would avoid also a HTML injection. Say that in your final result as another answer suggests has this:
<input type="hidden" name="point1" value="4" />
Anyone can edit the html code and change this value to lets say:
<input type="hidden" name="point1" value="400000" />
And then submit it. As you would not check, the value of the point it would not be the right one.
I have this:
<form method="post" id="kl" action="step2.php">
<input type="radio" name="rubrik" value="bussines"></input>
<input type="radio" name"rubrik" value="private"></input>
<input type="image" value="submit" src="/images/submit.png" alt="Submit" />
</form>
What i bassicaly want is: When the second radio button is checked, to submit the form to step2a.php, a different file. How can i do this? Jquery, Javascript, php?
You could do this with JavaScript (bind a submit listener that checks the value of the radio button and then sets the action property of the form), but it would be simpler and more reliable to do something (server side) along the lines of:
<form ... action="step-selector.php">
and
<?php
if (isset($_POST['rubrik']) && $_POST['rubrik'] == 'bussines') {
include('step2.php');
} elseif (isset($_POST['rubrik']) && $_POST['rubrik'] == 'private') {
include('step2a.php');
} else {
include('error-state.php');
}
?>
you can do this by modifying the Form into:
<form method="post" id="kl" action="step2.php">
<input type="radio" class="radio" rel="step2.php" name="rubrik" value="bussines"></input>
<input type="radio" class="radio" rel="step2a.php" name"rubrik" value="private"></input>
<input type="image" value="submit" src="/images/submit.png" alt="Submit" />
</form>
I added rel attribute to radio buttons. each has a value of the url. I also added a class to get the element with jQuery.
Now, you will need some Javascript, i will use jQuery code:
$('.radio').click(function (){
rad = $(this);
radRel = rad.attr('rel');
$('form#kl').attr('action', radRel);
});
There are multiple ways of doing it, depending on what you want exactly.
Check this one out, it might help you get there; Radio Button to open pages
You can use form.submit() as onclick-handler (not onchange) and change the action, too.
<input type="radio" name"rubrik" value="private" onclick="this.parentNode.action='yourOtherFile.php'; this.parentNode.submit()"></input>
If I make a long story very short, I have a short form I've made (an input, a select, and three checkboxes). I've made a function on a button that can dynamically add multiple instances of this form on my page. It saves it as an array (i.e. the input name is name="checkbox[]") which will save fine in my database. The problem I run into is I may have 6 instances of this form, but only some of the boxes are checked. So, I may have 6 text inputs, 6 select inputs, but maybe only 3 checkbox inputs. Since it only has 3 inputs, the array is only 3 pieces of data and when I run a for() statement it doesn't accurately save this information and tie it to the correct record.
I thought that maybe I could have a hidden input that will get its value assigned through javascript, but I don't know how to reference the checkboxes appropriately (you can't do id="blahblah[]" right?)
Sad and Confused,
ImmortalFirefly
I am not sure I have caught your drift on this one, but consider this:
<?php
var_dump( $_POST )
?>
<form name=form0 method= post action = "">
<input type=checkbox name=checkbox[0][0] />
<input type=checkbox name=checkbox[0][1] />
<input type=checkbox name=checkbox[0][2] />
<input type = submit>
</form>
Then another form is added
<form name=form1 method= post action = "">
<input type=checkbox name=checkbox[1][0] />
<input type=checkbox name=checkbox[1][1] />
<input type=checkbox name=checkbox[1][2] />
<input type = submit>
</form>
Mock that up in html and POST it back to a webpage and see how it works, you can iterate through th post value to see which form was sent and which box checked, or put it all in one single form.
<?php
var_dump( $_POST )
?>
<form name=form0 method= post action = "">
<input type=checkbox name=checkbox[0][0] />
<input type=checkbox name=checkbox[0][1] />
<input type=checkbox name=checkbox[0][2] />
<input type = submit>
Then another series of checkboxes is added :
<input type=checkbox name=checkbox[1][0] />
<input type=checkbox name=checkbox[1][1] />
<input type=checkbox name=checkbox[1][2] />
close off the form
<input type = submit>
</form>
I'm getting database from database and each row has a id and im showing it like this in html
<td><input type="checkbox" value"1">test1</td></tr>
<td><input type="checkbox" value"2">test2</td></tr>
and so on...
now lets say that user checked ten check boxes out of 15 and then clicked submit .
how to get values of those boxes in php???
Your checkboxes need to have a name & a value attribute:
<input type='checkbox' name='test1' value='1'> Test1
<input type='checkbox' name='test2' value='1'> Test2
then when that is posted you can access the values in PHP via $_POST:
$test1 = $_POST['test1']
$test2 = $_POST['test2']
Keep in mind that the values will only be returned if the box is checked, so most likely instead of the above PHP, you're more than likely just going to want to check if the value exists.
give the checkboxes names:
<td><input type="checkbox" value"2" name='test'>test2</td></tr>
Than in php just read the request
$test = $_REQUEST['test']
if the OP doesn't have these checkboxes inside of a , any amount of PHP code will make absolutely no difference (FROM Blender)
they will be in either the $_GET or the $_POST array
Try this way..
<form action="#" method="post">
<input type="checkbox" name="check_list[]" value="1"><label>Test 1</label><br/>
<input type="checkbox" name="check_list[]" value="2"><label>Test 1</label><br/>
<input type="checkbox" name="check_list[]" value="3"><label>Test 3</label><br/>
<input type="submit" name="submit" value="Submit"/>
</form>
<?php
if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected){
echo $selected."</br>";
}
}
}
?>