I have the below code. It's a function creating a from containing a dropdown menu and a selectionbox, and is being called from a different page).
<?php
require_once 'forbindtilDB.php';
function populerDropdownsOpretProgram()
{
global $mysqliOOP;
$stmt = $mysqliOOP->prepare("SELECT DISTINCT primaer FROM oevelser ORDER BY primaer ASC");
?>
<form action="temp.php" method="post">
<select multiple>
<option value="0">Vælg ønskede øvelse(r)</option>
<?php
$stmt->execute();
$stmt->bind_result($primaereMuskelgruppe);
while ($stmt->fetch())
{
?>
<option value = "<?php echo $primaereMuskelgruppe;?>" >
<?php echo $primaereMuskelgruppe;
?>
</option>
<?php
}
$stmt->close();
?>
</select>
<?php
$stmt = $mysqliOOP->prepare("SELECT antalreps FROM antalreps ORDER BY antalreps ASC");
?>
<select>
<option value="0">Vælg ønskede antal gentagelser</option>
<?php
$stmt->execute();
$stmt->bind_result($antalreps);
while ($stmt->fetch())
{
?>
<option value = "<?php echo $antalreps;?>" >
<?php echo $antalreps;
?>
</option>
<?php
}
$stmt->close();
?>
</select>
<input type="submit">
</form>
<?php
}
I want to post the user input on a different page (currently temp.php), but I don't know how to handle the fact that the form contents is variables fetched by a mysqli call.
So far I've tried different versions of the below on the temp.php-page
<?php
echo $_POST[$antalreps];
?>
<?php
echo $_POST[$primaereMuskelgruppe];
?>
But I'm getting errors stating that there is undefined variables (antalreps and primaeremuskelgruppe) and undefined indexes...
Also, there's the added complexity that the selection box may return more than one result.
Pretty sure echo $_POST[$antalreps]; etc. is the wrong way to go about this, but I haven't been able to figure out alternatives...
Any hints?
You should add 'name' to your select and then use it.
For example instead of:
<select>
you should have:
<select name="yourname">
and then in php to display its value use:
echo $_POST['yourname'];
and for select multiple you can use:
<select name="othername[]">
and then in PHP use it as array:
foreach ($_POST['othername'] as $item) {
echo $item;
}
of course where I put yourname and othername you should put descriptive name such as colours for color box and similar
Related
I have a function having two dropdown list and one submit button, the user must choose a value from dropdown list A and a value from dropdown list B so he can submit them.
I knew that I need to have an attribute in the value section as follows
<select name="students" id="students">
<?php
while($data = mysqli_fetch_array($selectstudent1)) {
$name=$data['name'];
$idname =$data['user_id_student'];
?>
<option name="studentdd" value="<?php echo $idname; ?>"><?php
echo $name; ?></option>
<?php } ?>
</select>
and the second dropdown list here
<select name="groups" id="groups">
<?php
while($data1 = mysqli_fetch_array($selectgroup1)) {
$groupschoose = $data1['group_number']; ?>
<option name="nogroup" value="<?php echo $groupschoose; ?>">
<?php echo $groupschoose; ?></option>
<?php }//while ?>
</select>
so, i need to get the "php echo $idname" (student id) from A dropdown list
and "php echo $groupschoose; " (group id) form drop downlist B
so I tried this PHP code as follows:
<?php
if(isset($_POST['addmember'])){
//addmember is id for dropdownlists form
$groupnum= $_POST['nogroup'];
$stuid=$_POST['studentdd'];
echo $groupnum;
echo $stuid ;
$sq="SELECT user_id_student FROM student WHERE
user_id_student=$stuid ";
$sq1 = mysqli_query($con,$sq);
while ($sq2 = mysqli_fetch_array($sq1)) {
$tes = $sq2['user_id_student'];
if( $tes == $stuid){
$sqlmem="UPDATE student
set group_id= $groupnum
WHERE user_id_student=$stuid";
$resultmem = mysqli_query($con,$sqlmem);}
}
}
?>
But it won't work, and I'm not sure where is the missing section
I hope my Q is clear and I'm sorry if it's little bit long, but i'm looking for your help, thank you!!
I have a dropdown box where the data is coming from my database. I want to send the selected drop-down item to the controller action method. And then I will send these values to the model to do the further works. Here, the problem is I have two database values in a single item of drop-down box. And, I am not figuring out how to send those two the method. Here is my code given below,
<select name='select'>
<option selected disabled>Choose Stations</option>
<?php foreach ($get_stations as $get_stations_item): ?>
<option>Station <?php echo $get_stations_item['sourcestationid']; ?> - Station <?php echo $get_stations_item['destinationstationid']; ?></option>
<?php endforeach; ?>
</select>
From this dropdown items, I want to send the sourcestationid and destinationstationid separately as 2 parameters to my controller action method. Here is my controller code though this is not correct way I think,
function getdata(){
$iotdata['test'] = $this->input->post('select2');
//rest of the code according to the source and destination id item
}
Thanks in advance.
You haven't kept the variable inside option's value tag.
<select name='select'>
<option selected disabled>Choose Stations</option>
<?php foreach ($get_stations as $get_stations_item): ?>
<option value="<?php echo $get_stations_item['sourcestationid'].','.$get_stations_item['destinationstationid']; ?>">Station <?php echo $get_stations_item['sourcestationid']; ?> - Station <?php echo $get_stations_item['destinationstationid']; ?></option>
<?php endforeach; ?>
</select>
Also here at PHP end, you can explode the string into an array and store it further into db.
<?php
$select = explode(',', $select)
print_r($select); // this will have your two values
?>
Thanks #BitsPlease for our suggestion. Your idea is working. But I need to do a small change to get it instantly. My select tag needs to be under form tag to get them without reloading the page again. Here is my view,
<form method="post" accept-charset="utf-8" action="<?php echo site_url("controller/action"); ?>">
<select name='select' onchange="this.form.submit()">
<option selected disabled>Choose Stations</option>
<?php foreach ($get_stations as $get_stations_item): ?>
<option value="<?php echo $get_stations_item['sourcestationid'].','.$get_stations_item['destinationstationidr']; ?>">Station <?php echo $get_stations_item['sourcestationid']; ?> - Station <?php echo $get_stations_item['destinationstationidr']; ?></option>
<?php endforeach; ?>
</select>
</form>
Here is the php code,
$select = explode(',', $this->input->post('select'));
print_r($select)
I am trying to pass the variable from my database to an HTML code but it isn't working. If I enter in a numeric value for my Select option it will work but it doesn't work with me trying to reference my class_id. The code I'm basing mine off of is found here. I am writing this code on wordpress also.
global $wpdb;
$result = $wpdb->get_results("SELECT course FROM class;");
echo "
<form>
<select name='department' onchange ='showUser(this.value)'>";
foreach($result as $results){
echo "<option selected value='$results->class_id'>$results->course</option>";
}
echo" </select>
</form>";
Try not to mix your HTML and PHP together, I've fixed a few things for you below. You need to select class_id in your query to use it as the option value, you can't use selected on all options as well. You may want to check if there are actually any results before looping over them too.
<?php
global $wpdb;
$results = $wpdb->get_results("SELECT class_id, course FROM class;");
?>
<form>
<select name="department" onchange="showUser(this.value)">
<?php foreach( $results as $result ): ?>
<option value="<?= $results->class_id ?>"><?= $results->course ?></option>
<?php endforeach ?>
</select>
</form>
global $wpdb;
$result = $wpdb->get_results("SELECT course FROM class;");
echo '<form><select name="department" onchange="showUser(this.value)">';
foreach($result as $results){
echo '<option selected value="'.$results->class_id.'" >'.$results->course.'</option>';
}
echo '</select></form>';
it is easy and simple for understanding use this code it's working prefect
<?php global $wpdb; $result = $wpdb->get_results("SELECT course FROM class;"); ?>
<form>
<select name='department' onchange ='showUser(this.value)'>
<?php foreach($result as $results){ ?>
<option selected value='<?php echo $results->class_id; ?>'><?php echo $results->course; ?> </option>
<?php } ?>
</select>
</form>
on calculatePC.php, I have this code to display the finish_product
Select product:
<select class="itemTypes">
<?php while ($row1 = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row1['finish_product']; ?>">
<?php echo $row1['finish_product']; ?>
</option>
<?php } ?></select>
<br/><br/>
Let's say I have chosen Table as the finish_product.
On docalculate.php, I would like to display what I've chosen based on the dropdown list I've selected.
I tried this but there is error.
<?php echo $_POST['finish_product'] ?>
May I know how to display the result?
This doesn't exist:
$_POST['finish_product']
because you don't have a form element named "finish_product" in your markup. Add that name to the form element:
<select name="finish_product" class="itemTypes">
You need to do two things:-
Create a form before select and give it an action
give name attribute to your select box, then only you can get data in $_POST
So do like below:-
<form method="POST" action = "docalculate.php"> // give the php file paht in action
<select class="itemTypes" name="finish_product"> // give name attribute to your select box
<?php while ($row1 = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row1['finish_product']; ?>">
<?php echo $row1['finish_product']; ?>
</option>
<?php } ?></select>
<br/><br/>
<input type ="submit" name="submit" value ="Submit">
</form>
Now on docalculate.php:-
<?php
echo "<pre/>";print_r($_POST['finish_product']); // to see value comes or not
Note:- through Jquery its also possible. Thanks
First time poster and wet-behind-the-ears programmer here, hope you guys can help me out!
I'm trying to create a dropdown list using php/html, and to populate it using a mysqli-call.
Here's what I've got so far:
<?php
require_once 'forbindtilDB.php';
$result = performQuery("SELECT DISTINCT primaer FROM oevelser");
FOLLOWING TWO LINES ARE ONLY TO CHECK RESULT 'MANUALLY' IN UI:
$row = mysqli_fetch_array($result);
echo $row['primaer'];
?>
<html>
<body>
<form>
<select>
<option value="0">Please Select</option>
<?php
while($row = mysql_fetch_assoc($result) )
{
?>
<option value = "<?php echo($row['primaer'])?>" >
<?php echo($row['primaer']) ?>
</option>
<?php
}
?>
</select>
</form>
</body>
</html>
The dropdown is displayed, but it's not getting any contents besides 'Please select' (should be two options at this stage, more to come...)
Any hints on this?
Also, since I'm using DISTINCT to get data from a specific column in the table, should I use something else than 'fetch_assoc' to get my result?
Any input will be appreciated!