$POST_ to return array from multiple checkbox - php

Hi I have looked at other answers and they said to add name=checkbox[] to get the array to return but it doesn't appear to working.
The HTML is:
<select class="select" multiple="multiple" name="suburb[]" id="suburb">
<option selected="selected" name="suburb[]" value="Southbank">Southbank</option>
<option selected="selected" name="suburb[]" value="Melbourne">Melbourne</option>
<option selected="selected" name="suburb[]" value="Docklands">Docklands</option>
<option selected="selected" name="suburb[]" value="South Melbourne">South Melbourne</option>
<option selected="selected" name="suburb[]" value="West Melbourne">West Melbourne</option>
<option selected="selected" name="suburb[]" value="Point Cook">Point Cook</option>
<option selected="selected" name="suburb[]" value="Sanctuary Lakes">Sanctuary Lakes</option>
<option selected="selected" name="suburb[]" value="Truganina">Truganina</option>
<option selected="selected" name="suburb[]" value="Williams Landing">Williams Landing</option>
PHP code is:
$message .= "<tr><td><strong>Interested Suburbs:</strong> </td><td>" . strip_tags($POST_['suburb']) . "</td></tr>";

$_POST['suburb']
Is an array not a string also you mispelled it its $_POST, so you would need to loop through it to post like so:
$message .= "<tr><td><strong>Interested Suburbs:</strong> </td><td>";
foreach ($_POST['suburb'] as $suburb)
{
$message .= strip_tags($suburb) . "<br />\n";
}
$message .= "</td></tr>";

Works fine: you just need to use $_POST and print_r the array to see its contents. Try this, and you'll see it works fine.
By the way, you don't need a name attribute on your options
<form method="post" action="<?=$PHP_SELF?>">
<select name="suburb[]" class="select" multiple="multiple" id="suburb">
<option selected="selected" value="Southbank">Southbank</option>
<option selected="selected" value="Melbourne">Melbourne</option>
<option selected="selected" value="Docklands">Docklands</option>
<option selected="selected" value="South Melbourne">South Melbourne</option>
<option selected="selected" value="West Melbourne">West Melbourne</option>
<option selected="selected" value="Point Cook">Point Cook</option>
<option selected="selected" value="Sanctuary Lakes">Sanctuary Lakes</option>
<option selected="selected" value="Truganina">Truganina</option>
<option selected="selected" value="Williams Landing">Williams Landing</option>
</select>
<button type="submit">test</button>
</form>
<?php
print_r($_POST['suburb']);
?>

The variable you're looking for is $_POST, not $POST_; see the PHP reference on $_POST for more details.
Otherwise, from what you have shown, it's probably fine. Add the HTML for your form tag as well if things still don't work.

Related

how to show gender using simple html dom parser in php?

i'm unable for success in how to show gender and state from below coading
please solve this.
$gender = $html->getElementById("cpBody_rbtnListGender")->getAttribute("value");
$state = $html->getElementById("cpBody_ddlState")->getAttribute("value");
this is for gender
<span id="cpBody_rbtnListGender" class="form-control pull-left radio-input"><input id="cpBody_rbtnListGender_0" type="radio" name="ctl00$cpBody$rbtnListGender" value="MALE" checked="checked" tabindex="3"><label for="cpBody_rbtnListGender_0">Male</label><input id="cpBody_rbtnListGender_1" type="radio" name="ctl00$cpBody$rbtnListGender" value="FEMALE" tabindex="3"><label for="cpBody_rbtnListGender_1">Female</label><input id="cpBody_rbtnListGender_2" type="radio" name="ctl00$cpBody$rbtnListGender" value="TRANSGENDER" tabindex="3"><label for="cpBody_rbtnListGender_2">Transgender</label><input id="cpBody_rbtnListGender_3" type="radio" name="ctl00$cpBody$rbtnListGender" value="OTHER" tabindex="3"><label for="cpBody_rbtnListGender_3">Other</label></span>
and this is for state
<div class="input-group">
<label class="control-label label-fixed">State</label>
<select name="ctl00$cpBody$ddlState" id="cpBody_ddlState" tabindex="15" class="form-control">
<option value="0">--SELECT STATE--</option>
<option value="1">ANDAMAN & NICOBAR ISLANDS</option>
<option value="2">ANDHRA PRADESH</option>
<option value="3">ARUNACHAL PRADESH</option>
<option value="4">ASSAM</option>
<option value="5">BIHAR</option>
<option value="6">CHANDIGARH</option>
<option value="7">CHHATTISGARH</option>
<option value="8">DADRA & NAGAR HAVELI</option>
<option value="9">DAMAN & DIU</option>
<option value="10">DELHI</option>
<option value="11">GOA</option>
<option value="12">GUJARAT</option>
<option value="13">HARYANA</option>
<option value="14">HIMACHAL PRADESH</option>
<option value="15">JAMMU AND KASHMIR</option>
<option value="16">JHARKHAND</option>
<option value="17">KARNATAKA</option>
<option value="18">KERALA</option>
<option value="19">LAKSHADWEEP</option>
<option value="20">MADHYA PRADESH</option>
<option value="21">MAHARASHTRA</option>
<option value="22">MANIPUR</option>
<option value="23">MEGHALAYA</option>
<option value="24">MIZORAM</option>
<option value="25">NAGALAND</option>
<option value="26">ORISSA</option>
<option value="27">PONDICHERRY</option>
<option value="28">PUNJAB</option>
<option selected="selected" value="29">RAJASTHAN</option>
<option value="30">SIKKIM</option>
<option value="31">TAMIL NADU</option>
<option value="32">TELANGANA</option>
<option value="33">TRIPURA</option>
<option value="34">UTTAR PRADESH</option>
<option value="35">UTTARAKHAND</option>
<option value="36">WEST BENGAL</option>
</select>
</div>
state and gender is not showing using getAttribute("value");
You could make use of find.
One option to get the state could be to use the id and get the selected option #cpBody_ddlState option[selected]
$state = $html->find('#cpBody_ddlState option[selected]', 0)->plaintext;
echo $state; //RAJASTHAN
To get the selected radio button you might use #cpBody_rbtnListGender input and loop though the items until the attribute checked matches:
foreach ($html->find('#cpBody_rbtnListGender input') as $input) {
if ($input->hasAttribute("checked")) {
echo $input->getAttribute("value"); // MALE
}
}
idk why you're doing this with php.'
in javascript u can create a p to pass the value to it:
<body onload="pageloaded()">
<p id="display"></p>
in javascript:
var p = document.getElementById("display");
var value = document.getElementById("cpBody_rbtnListGender").value;
function pageloaded(){
p.innerHTML = value;
}
u can do the same for ur other needs

Get multiple select data from Bootstrap Select using PHP

I'm using bootrap-select plugin
1: https://github.com/snapappointments/bootstrap-select/ I'm trying to grab data from it. I try so many ways but it doesn't work out. So the input is like this:
<div class="col-9">
<select id="generals" name="generals" class="form-control kt-selectpicker" multiple title="Choose one of the following...">
<optgroup label="Passport">
<option value="passport_number">Number</option>
<option value="passport_date">Date of issuance</option>
<option value="passport_expired">Date of expiry</option>
<option value="passport_office">Issuing Office</option>
</optgroup>
<optgroup label="Personal">
<option value="applicant_id">Applicant Id</option>
<option value="first_name">First Name</option>
<option value="first_name">Middle Name</option>
<option value="last_name">Last Name</option>
<option value="address">Address</option>
<option value="crew_id">Crew Id</option>
<option value="email">Email</option>
<option value="mobile_number">Mobile Number</option>
</optgroup>
</select>
</div>
I try to grab using PHP foreach and for loops, implode or explode but it doesn't work since it said it does not an array. When i see the Header data sent is like this:
so how can i get this data? I'm using Ajax to send data. Here's the Fiddle if you want to see it: https://jsfiddle.net/4u6xc9kd/
It's easy, you just put the input name like this name="generals[]" in your input select and the use foreach like this:
$generals = '';
if (isset($_POST['generals'])){
foreach ($_POST['generals'] as $value){
$generals .= $value . ', ';
}
}

How to set Dropdown List default value based on user selection

I have a dropdown list. However i want to set a default based on user selection. Therefore the default value is not consistent. What can i do to achieve this? I have set $country = $_POST['country']; as user selection.
<td>Country:</td>
<td colspan="2"><select name="country">
<option value="93">93-Afghanistan</option>
<option value="355">355-Albania</option>
<option value="213">213-Algeria</option>
<option value="1-684">1-684-American Samoa</option>
<option value="376">376-Andorra</option>
<option value="244">244-Angola</option>
<option value="1-264">1-264-Anguilla</option>
<option value="672">672-Antarctica</option>
<option value="1-268">1-268-Antigua and Barbuda</option>
<option value="54">54-Argentina</option>
<option value="374">374-Armenia</option>
<option value="297">297-Aruba</option>
<option value="61">61-Australia</option>
</select>
To add on. There are 200 over options (I never list all down) so i hope to get a convenience way to achieve this
You can use 'selected' for relevant option
<option value="93" <?php if($country==93) echo "selected" ?> >93-Afghanistan</option>
<option value="355" <?php if($country==355) echo "selected" ?> >355-Albania</option>
like this add if conditon for each option.
Try using session for it.
The php code:
session_start();
$_SESSION['selectedCountry'] = $_POST['country'];
Then you can use JQuery to make dropdown selected:
$(document).ready(function(){
$(function() {
$("#country").val("<?php echo $_SESSION['selectedCountry'];?>");
});
})
Please try out this ..
HTML :-
<td>Country:</td>
<td colspan="2"><select id="country" name="country">
<option value="93">93-Afghanistan</option>
<option value="355">355-Albania</option>
<option value="213">213-Algeria</option>
<option value="1-684">1-684-American Samoa</option>
<option value="376">376-Andorra</option>
<option value="244">244-Angola</option>
<option value="1-264">1-264-Anguilla</option>
<option value="672">672-Antarctica</option>
<option value="1-268">1-268-Antigua and Barbuda</option>
<option value="54">54-Argentina</option>
<option value="374">374-Armenia</option>
<option value="297">297-Aruba</option>
<option value="61">61-Australia</option>
</select>
</td>
JQuery :-
$(document).ready(function(){
$(function() {
$("#country").val("<?php echo $_POST['country'];?>");
});
})
Try this...
<?php
(isset($_POST["country"])) ? $country = $_POST["country"] : $country=93;
?>
<form action='stackover.php' method='POST'>
<select id="country" name="country">
<option <?php if ($country == 93 ) echo 'selected' ; ?> value="93">93-Afghanistan</option>
<option <?php if ($country == 355 ) echo 'selected' ; ?> value="355">355-Albania</option>
<option <?php if ($country == 213 ) echo 'selected' ; ?> value="213">213-Algeria</option>
</select>
<input type="submit" value="Submit">
</form>

PHP: select form menu list error

Hi my select menu list is not working.
<?php
<tr>
<select name="package">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
if(isset($_POST['package'])) echo "<option>" . $_POST['package'] . "</option>";
</tr>
?>
Help, would be appreciated.
Many thanks!
you have mixed php with html and also you didnt close SELECT tag.
try that:
<tr>
<select name="package">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
<?php
if(isset($_POST['package'])){ echo "<option value='".$_POST['package'] ."'>" . $_POST['package'] . "</option>"};
?>
</select> <-----you missed this
</tr>
when using php and html you need either :
<?php echo "<select name='package'>
<option value='volvo'>Volvo</option>
....................................";
?>
or the code i provided above without before and after html code.
You can't use html inside php directory. If you want to use html mixed with php either do it using echo or outside <?php //inside ?> //outside. Here try this.
<tr>
<select name="package">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
<?php
if(isset($_POST['package']))
echo "<option>" . $_POST['package'] . "</option>";
?>
</tr>

Retain drop-down list values in PHP without using array

I have a drop-down list containing all the countries in the world. The first few countries are:
Country: <select name="Country">
<option value="Afghanistan">Afghanistan</option>
<option value="Aland Islands">Aland Islands</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
<option value="American Samoa">American Samoa</option>
<option value="Andorra">Andorra</option>
<option value="Angola">Angola</option>
<option value="Anguilla">Anguilla</option>
</select>
Forming an array would require over 200 elements. Is there a way to retain the drop-down value in PHP after a form post without using an array? Thank you.
You really are best using an array, it won't take any real noticable time to iterate through 200 items
Something like this should keep the selected item after post
<select name="country" method="POST">
<?php
for($i=0;$i<count($countries);$i++)
{
$selected="";
if($countries[$i]==$_POST['country'])
{
$selected="selected";
}
?><option <?php echo $selected;?> value="<?php echo $countries[$i];?>"><?php echo $countries[$i];?></option><?php
}
?>
</select>

Categories