Can't retrieve radio button values from multiple radio groups - radio-button

In my form, I have x groups of radio buttons generated:
for ( $j = 0; $j < $x; $j++ ) {
<input type="radio" name="gender<?php echo $x; ?>" value="male" />Male
<input type="radio" name="gender<?php echo $x; ?>" value="female" />Female
...
}
When, after submit, I try to retrieve these values:
for ( $j = 0; $j < $x; $j++ ) {
echo $_POST['gender'.$j];
...
}
I get these error messages:
Notice: Undefined index: gender0 in ...
Notice: Undefined index: gender1 in ...
Notice: Undefined index: gender2 in ...
Yet I see these radio groups in my markup correctly defined:
<input type="radio" value="male" name="gender0">Male
<input type="radio" value="female" name="gender0">Female
...
<input type="radio" value="male" name="gender1">Male
<input type="radio" value="female" name="gender1">Female
So why these error messages?

Just make a var_dump($_POST); on the beginning of your receiving script, then you’ll see exactly what you get.
And you are aware that you get values only for radio buttons that are actually checked, right?
So if you can not be certain if the user will have a radio button checked per group, then check whether that POST entry is available before accessing it, using isset or empty.
Btw., an even more convenient way to deal with data like this, is to name the form fields in a way that gets you an array in PHP in the first place:
<input type="radio" name="gender[0]" value="male" />Male
<input type="radio" name="gender[0]" value="female" />Female
<input type="radio" name="gender[1]" value="male" />Male
<input type="radio" name="gender[1]" value="female" />Female
– that will get you an array in $_POST['gender'], that you can loop through with a foreach loop like this:
if(isset($_POST['gender']) && is_array($_POST['gender'])) {
foreach($_POST['gender'] as $index => $value) {
// …
}
}

Related

Undefined index eror in $_POST

When $_POST submitted anythings working well, But i got this eror Notice: Undefined index: trackingbymobile in /home/../sot_options.php on line ۱۸
if (isset($_POST['optionsformbtn'])&& ($_POST['optionsform'] == 'optionsform')&& ($_POST['trackingbymobile']!=="")){
$wpdb->query($wpdb->prepare( "UPDATE $sotoptions SET option_value = %s WHERE option_name = %s", $_POST['trackingbymobile'], 'trackingbymobile' ));
}
<form name="optionsform" id="optionsform" action="" method="POST">
<input type="checkbox" name="trackingbymobile" id="trackingbymobile">
<input class="sotadminpanelbtn" type="submit" name="optionsformbtn">
</form>

Why do I keep seeing the undefined index error for line 13 ('file')?

I am trying to create a simple drag and drop page using php. When I click the "submit" button, I get an error: Notice: Undefined index: file in C:\xampp\htdocs\phpfiles\DragAndDrop\includes\gallery-upload.inc.php on line 13. Why is it not reading the "file" index from the htmlphp?
Here is the HTML and PHP code:
enter code here
<!--continuted from HTML page-->
</section>
<?php
$_SESSION['username'] = "Admin";
if (isset($_SESSION['username'])) {
echo '<div class="gallery-upload">
<form action="includes/gallery-upload.inc.php" method="post" enctype="mutipart/form-data">
<input type="text" name="filename" placeholder="File Name...">
<input type="text" name="filetitle" placeholder="Image Title...">
<input type="text" name="filedesc" placeholder="Image description...">
<input type="file" name="file">
<button type="submit" name="submit">Upload</button>
</form>
</div>';
}
?>
</main>
</body>
</html>
<!--and the php page-->
<?php
if (isset($_POST['submit'])) {//checks submit form and posts the info
$newFileName = $_POST['filename'];
if (empty($_POST['filename'])) { //if filename is emptly
$newFileName = "gallery"; //if filename is empty, generates name
} else {
$newFileName = strtolower(str_replace(" ", "-", $newFileName)); //if spaces are in the name, creates stringholder
}
$imageTitle = $_POST['filetitle'];
$imageDesc = $_POST['filedesc'];
$file = $_FILES['file'];
}
?>
You have a typo:
mutipart/form-data
Should be
multipart/form-data

Selenium: Select RadioButton in Label by Value (or Label Text)

I'm new to Selenium and want to select a RadioButton inside a group of labels, see Screenshot. The problem is, that all <input> elements have the same name! So I have to select them by the value (or by the label text?)...
I'm using the Java API for Selenium.
** As HTML **
<table width="100%" border="1">
...
<label>
<input type="radio" name="AktarmaSekli" value="SP" checked class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">Sipariş Planı</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="DF" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">DAG Fatura Bilgileri</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="ATR" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">ATR Bilgileri</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="AITM" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">AİTM Bilgileri</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="COC" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">CoC Bilgileri</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="Bakim" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">Bakım Faturaları</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="AF" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">Araç Bilgileri</label></td></tr>
</table>
Good to know you have solution.. below one may also helps..
If you are trying to click on specific button, if it has value say 'ATR' you can build xpath (or css selector) simply //input[#value='ATR'] or //input[contains(text(),'ATR Bilgileri')]
off-course other ways also there to find required element..
Thanks,
Murali
I got it to work with the following code!!!
private void selectRadioButtonByValue(WebDriver driver, String radioGroupName, String valueToFind) {
List<WebElement> radioGroup = driver.findElements(By.name(radioGroupName));
for (int i = 0; i < radioGroup.size(); i++) {
if(radioGroup.get(i).getAttribute("value").equals(valueToFind)) {
radioGroup.get(i).click();
return;
}
}
throw new org.openqa.selenium.NotFoundException("Element wasn't found by value!");
}
In Page Object Pattern it will be:
#FindBy(xpath = "//*[contains(text(),'ATR Bilgileri')]")
WebElement radioATR;

Insert function using a radio button

So my problem is that I am unable to insert the data into the selected table using the radio buttons. The radio buttons are used to tell us which table we need to access and to add the data. I thought that I had it correct but it did not work. Here is what I have. Any kind of help would be greatly appreciated.
<h1 align="center">Event</h1>
<h1 align="center">Storage</h1>
<form action="set_event.php" method="post">
<p>
<p>
<input type="submit" name="submit" value="Set Items">
</p>
<p>
<br>
<label>
<input type="radio" name="storage" value="concourse_stairs_s" id="s1">
Concourse Stairs</label>
<br>
<label>
<input type="radio" name="storage" value="bat_cave_s" id="s2">
Bat Cave</label>
<br>
<label>
<input type="radio" name="storage" value="fireside_s" id="s3">
Fireside Storage</label>
<br>
</p>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
require ('mysqli_connect.php'); // Connect to the db.
$errors = array(); // Initialize an error array.
}
if (isset($_REQUEST['bat_cave_s']))
{
$q = "INSERT INTO bat_cave_s (item_name, check_in) VALUES ('test', NOW())";
$r = #mysqli_query ($dbc, $q); // Run the query.
}
else if(isset($_REQUEST['concourse_stairs_s']))
{
$q = "INTO concourse_stairs_s (item_name, check_in) VALUES ('test', NOW())";
$r = #mysqli_query ($dbc, $q); // Run the query.
}
else if(isset($_REQUEST['fireside_s']))
{
$q = "INSERT INTO fireside_s (item_name, check_in) VALUES ('test', NOW())";
$r = #mysqli_query ($dbc, $q); // Run the query.
}
NOW())";
Your PHP is checking the wrong thing.
You need to check the value of $_REQUEST['storage'].
if($_REQUEST['storage'] == 'bat_cave_s') {

Select a default radio button from another page

I am trying to place a series of links on one page that, when clicked, will select a default value of a radio group on another page. The perfect example of this is here. By clicking one of the blue links on the far right, they define the default value of the radio group on the next page. That is exactly what I am trying to do.
Can anyone help me understand how they did that?
This isn't 'checking' the box on the other end. The link that you're clicking contains a variable in the URL (GET variable). When the new page loads, it's likely using a server-side scripting language to test for each radio button.
In this case, ?default_amt=5 is what is attached for the $5 contribution. Then, on the page:
<input type="radio" checked="checked" value="5" name="amount" id="amt_preset_1" onclick="BSD.contribution.clearother();" class="radiobutton">
Is the value for the radio button. Simply put something like this php code to 'check' it
<?php if ($_GET['default_amt'] == "5") {echo "checked=\"checked\"; } ?>
The links on the previous page could be this:
Set amount to 1 on new page
Set amount to 2 on new page
Set amount to 3 on new page
Set amount to 4 on new page
'newpage.html' would have the following:
<input type="radio" value="1" name="toPay" <?php if ($_GET['amount'] == "1") {echo "checked=\"checked\"; } ?>>
<input type="radio" value="2" name="toPay" <?php if ($_GET['amount'] == "2") {echo "checked=\"checked\"; } ?>>
<input type="radio" value="3" name="toPay" <?php if ($_GET['amount'] == "3") {echo "checked=\"checked\"; } ?>>
<input type="radio" value="4" name="toPay" <?php if ($_GET['amount'] == "4") {echo "checked=\"checked\"; } ?>>