POST to database an array variable with foreach - php

CASE 1
i have following code like this ,
<SELECT name="country[]">
<OPTION >India</OPTION>
<OPTION >Germany</OPTION>
<OPTION >France</OPTION>
<OPTION >United States</OPTION>
<OPTION >Switzerland</OPTION>
$country = $_POST['country'];
foreach($country as $a => $b) {
$simpan = mysql_query("insert into tablename(countryyyy) values('$country[$a]')");
}
why only data from first array who entered into database ?
the rest not entered into database ?
CASE 2
<INPUT type="text" name="txt[]">
<SELECT name="country[]">
<OPTION >India</OPTION>
<OPTION >Germany</OPTION>
<OPTION >France</OPTION>
<OPTION >United States</OPTION>
<OPTION >Switzerland</OPTION>
</SELECT>
$txtbox = $_POST['txt'];
$country = $_POST['country'];
foreach($country as $a => $b) {
$satuin = $txtbox[$a].$country[$a]."<br/>";
$simpan = mysql_query("insert into namatabel(negara) values('$satuin')");
}
if i have 2 array variable like above , how to concat all into one and post to database with as separator
data example who want to post to one of column :
india
india
germany
india
thanks in advance

Not sure what you want to do, but if you're using select box for multiple values then you need multiple="multiple" in select box something like this
<SELECT name="country[]" multiple="multiple">
<OPTION value="in">India</OPTION>
<OPTION value="de">Germany</OPTION>
<OPTION value="fr">France</OPTION>
<OPTION value="us">United States</OPTION>
<OPTION value="ch">Switzerland</OPTION>
</SELECT>

Related

PHP Country Option list adding "select" tag with checking String value?

I want to select a target country in html.
And this code should return for example "Turkey" from my db.
<?php echo $_SESSION['user']['www']?>
I have a country list, and I use it here in settings_myinfo.php :
<select id="profile-country" name="profile_country">
<option value="Select your Country">Select your Country</option>
<?php include($root."countries_optionlist.php");?>
</select>
countries_optionlist.php :
<option value="Afganistan">Afghanistan</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>
<option value="Antigua & Barbuda">Antigua & Barbuda</option>
<option value="Argentina">Argentina</option>
<option value="Armenia">Armenia</option>
<option value="Aruba">Aruba</option>
<option value="Australia">Australia</option>
<option value="Austria">Austria</option>
<option value="Azerbaijan">Azerbaijan</option>
<option value="Bahamas">Bahamas</option>
<option value="Bahrain">Bahrain</option>
<option value="Bangladesh">Bangladesh</option>
<option value="Barbados">Barbados</option>
and more...
I want to add selected tag to target country like this:
<option value="Turkey" selected>Turkey</option>
I did some research on Google and StackOverflow but I couldn't find any solution.
Could you provide me with a suggestion on how to achieve this?
In each option, you can check if the session variable matches that country and add the selected attribute.
<?php $user_country = $_SESSION['user']['www']; ?>
<option value="Afganistan" <?php if ($user_country == 'Afghanistan') echo 'selected'; ?>>Afghanistan</option>
<option value="Albania" <?php if ($user_country == 'Albania') echo 'selected'; ?>>Albania</option>
<option value="Algeria" <?php if ($user_country == 'Algeria') echo 'selected'; ?>>Algeria</option>
...
You can simplify this by putting all the country names in an array. Then you can loop like this:
<?php
$user_country = $_SESSION['user']['www'];
foreach ($countries as $country) { ?>
<option value="<?php echo $country; ?>" <?php if ($user_country == $country) echo 'selected'; ?>>
<?php echo htmlspecialchars($country); ?>
</option>
<?php
}

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

Pushing Data to a MySQL Database from a Form

I am trying to send data from an HTML form to a MySQL database in phpmyadmin. I have a database named bhs2018 and a table called game 1. Here are the contents of that table.
Here is my form:
<form name="game" action="insert.php" method="post">
<p> <select id="player" name = 'player'>
<option value="b">B</option>
<option value="n">N</option>
<option value="a">A</option>
<option value="c">C</option>
<option value="m">M</option>
<option value="j">J</option>
<option value="ja">Ja</option>
</select>
<select id="what" name = 'what'>
<option value="shoton">Shot on Cage</option>
<option value="shotoff">Shot off Cage</option>
<option value="goal">Goal</option>
<option value="countergoal">Goal on Counter</option>
<option value="countershot">Shot on Counter</option>
<option value="assist">Assist</option>
<option value="block">Block</option>
<option value="steal">Steal</option>
<option value="turnover">Turnover</option>
<option value="drawn">Ejection Drawn</option>
<option value="ejected">Ejected</option>
</select>
<select id="where" name = 'where'>
<option value="set">Set</option>
<option value="navy">Navy</option>
<option value="leftwing">1/2 side past 5</option>
<option value="rightwing">4/5 side past 5</option>
<option value="point">Point/3</option>
<option value="lefttwo">1/2 side 2 meter</option>
<option value="righttwo">4/5 side 2 meter</option>
<option value="1">6 on 5 1</option>
<option value="2">6 on 5 2</option>
<option value="3">6 on 5 3</option>
<option value="4">6 on 5 4</option>
<option value="5">6 on 5 5</option>
<option value="6">6 on 5 6</option>
</select>
<select id="quarter" name = 'quarter'>
<option value="q1">Quarter 1</option>
<option value="q2">Quarter 2</option>
<option value="q3">Quarter 3</option>
<option value="q4">Quarter 4</option>
</select>
<select id="time" name = 'time'>
<option value="0:30">0:30</option>
<option value="1:00">1:00</option>
<option value="1:30">1:30</option>
<option value="2:00">2:00</option>
<option value="2:30">2:30</option>
<option value="3:00">3:00</option>
<option value="3:30">3:30</option>
<option value="4:00">4:00</option>
<option value="4:30">4:30</option>
<option value="5:00">5:00</option>
<option value="5:30">5:30</option>
<option value="6:00">6:00</option>
<option value="6:30">6:30</option>
<option value="7:00">7:00</option>
</select>
Notes: <input type="text" id = 'notes' name = 'notes'>
<button type="submit" onclick="save()"> Save </button> </p>
</form>
Whenever I click my "Save" button, the insert.php script loads. Instead of echoing something, it just shows the code. Here is insert.php.
<?php
$con = mysqli_connect('127.0.0.1','root','password'(my actual password is here);
if(!$con){
echo 'Not Connected to Server';
}
if (!mysqli_select_db($con,'bhs2018')){
echo 'Not Selected';
}
$Player = $_POST['player'];
$Quarter = $_POST['quarter'];
$Time = $_POST['time'];
$Where = $_POST['where'];
$Notes = $_POST['notes'];
$What = $_POST['what'];
$sql = "INSERT INTO game1 (player,quarter,time1,where1,notes,what) VALUES ('$Player', '$Quarter', '$Time', '$Where','$Notes','$What')";
if(!mysqli_query($con,$sql)){
echo'Not Inserted';
}
else{
echo 'Inserted';
}
header('refresh:2; url=index.html');
?>
What is looking wrong with my code? Why does it not run the php script? Thank you so much!
Check your database. Your PRIMARY KEY player should be an integer like int(30) or bigint(200) but not varchar. Create a new column for PRIMARY KEY something like player_id and shift player to next column. Also when primary key is an integer, there is no manual insertion option for inputting it's value in your form. So make your PRIMARY KEY is set to AUTO_INCREMENT in case you don't want to insert it's value manually.

How to use index of loop in Post method PHP

I need your help! I am trying to save a variable in sql table using Php but I have problem. There are two questions in php, the first concern the continent and the second is depended from the continent. I want to use a loop to check which of the continents has been selected in the first question and then save the value of the second question. I hide the option of unchecked continent using some javascript code (I don't have problem).
The HTML code:
<form method="post" action="">
<fieldset><legend>Continents</legend>
<select id="q1" name="q1">
<option value="1">Africa</option>
<option value="2">Asia</option>
<option value="3">Australia</option>
<option value="4">America</option>
<option value="5">Europe</option>
</select>
<select id="q2" name="Africa">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Asia">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Australia">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="America">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Europe">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
</fieldset>
The Php code
$q1 = $_POST['q1'];
$continents = array("Africa","Asia", "Australia","America","Europe");
for ($i = 1; $i <= 5; $i++) {
if($q1 == $i) {
$q2 = $_POST[$continents[$i-1]]
}
}
Your array should be from 1 to 5 instead of 0 to 4 as you have values 1 to 5 in q1.
Alternatively, I suggest that change your HTML structure to get the continent value in a single line without using loop. You need to change the values of continent like,
<select id="q1" name="q1">
<option value="Africa">Africa</option>
<option value="Asia">Asia</option>
<option value="Australia">Australia</option>
<option value="America">America</option>
<option value="Europe">Europe</option>
</select>
And to get the value of selected continent use $_POST[$_POST['q1']]. For egs, $_POST['q1']=Asia, then $_POST['Asia'] will return the Asia's choice,
$q2= $_POST[$_POST['q1']];
change
$continents =
array(1 => "Africa",
2 => "Asia",
3 => "Australia",
4 => "America",
5 => "Europe");
or
if(($q1-1) == $i) {
$q2 = $_POST[$continents[$i]]
}
First, you may change your select continent HTML:
<select id="q1" name="q1">
<option value="Africa">Africa</option>
<option value="Asia">Asia</option>
<option value="Australia">Australia</option>
<option value="America">America</option>
<option value="Europe">Europe</option>
</select>
Then you could loop over your continents and get the answer:
$q1 = $_POST['q1'];
$continents = array("Africa","Asia", "Australia","America","Europe");
foreach($continents as $continent) {
if ($_POST['q1'] == $continent) {
$q2 = $_POST[$continent];
}
}
Now you have your answer to the second question in $q2.
How about this?
// Always try to seperate logic from your view
// Intialize data
$continents = array("Africa", "Asia", "Australia", "America", "Europe");
// Check for $_POST
if(isset($_POST["q1"])) {
foreach($continents as $id => $continent) {
if($_POST["q1"] == $id) {
// Do something special here
}
}
}
// Render HTML
<form method="post" action="">
<fieldset>
<legend>Continents</legend>
<select id="q1" name="q1">
<!-- Notice that I echo only variables not all of the html -->
<?php foreach($continents as $id => $continent) { ?>
<option value="<?php echo $id; ?>"><?php echo $continent; ?></option>
<?php } ?>
</select>
<select id="q2" name="Africa">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Asia">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Australia">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="America">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Europe">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
</fieldset>
Beautiful thing about my solution is that now you can create whatever array you want and your code will always work.
You can for example SELECT data from database and store them in $continents variable.
$query = "SELECT id, name FROM continents";
$result = mysql_query($query);
$continents = array();
while($row = mysql_fetch_assoc($result)) {
$continents[$row["id"]] = $row["name"];
}
Cool right? :)
I found the solution finally!!!
Replace
$q2 = $_POST[$continents[$i-1]]
with
$q2 = $_POST["{$continents[$i-1]}"];
That solution I wanted!!

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