I am looping over an array and passing these values to a select list as follows
<select class="form-control chzn-select">
<?php while($row = mysqli_fetch_array($query)){
echo "<option>" . $row['schoolname'] . "</option>";
}?>
</select>
I have to pass $row['schoolcode'] in <option value='here'>. How do I do that?
echo "<option value='" . $row['schoolcode'] . "'>" . $row['schoolname'] . "</option>";
What does this have to do with jQuery?
Are you trying to do the following:
echo '<option value="' . $row['schoolcode'] . '">' . $row['schoolname'] . '</option>';
Also: Try to separate logic from presentation. (A common way to do so is the MVC or MVVM way) It is not good to communicate with the database while building the html. (What happens on an error? You cannot redirect and will send an incomplete or wrong html)
Related
I have a <select> but for some reasons I need to echo two <option> inside. So, the code looks like something like this:
<select class="form-control" name="select2" id="select2">
echo "<option value='" . $row['sectionid'] . "'>" . $row['name'] . "</option>";
echo "<option hidden value='" . $row['id'] . "'>" . $row['id'] . "</option>";
</select>
The two <option> are under the same name which is select2.
$var=$_POST['select2'];
My question is how to $_POST one value of those?
From the exchange in the comments it sounds like you want the user to see $row['name'] and the $_POST['select2'] to have the value $row['id'].
You would get that result if you construct the <option> like so:
echo "<option value='" . $row['id'] . "'>" . $row['name'] . "</option>";
That would eliminate the need for the second hidden option.
It seems like you want to get $row['sectionid'] as well as $row['id']. For this you don't need to put 2 options and fetch their value independently. You can keep data-id attribute in the same option field to store $row['id'].Then you can combine both values and feed in one common hidden input field.
<form id="appform" name="appform"><input id="requireField" name="requireField" value="" type="hidden"><select type="text" id="city" name="city"><option value="31" data-id="2">Faridabad</option><option value="32" data-id="3">Delhi</option></select></form><script>$("#city").change(function(){var combined_value= $("#city").val()+" and " +$("#city option:selected").attr('data-id');alert("Value is "+$("#city").val()+" data id is "+$("#city option:selected").attr('data-id'));$("#requireField").val(combined_value);});</script>
I am making a page that sends data in the POST and refreshes when you click on a select list option. The data is retrieved from a database. I am using the this.form.submit() function to send the variable when you click on an option. However, for some reason, it doesn't send the variable in the value="" of the option box but the text in between the ><. Below is the piece of code I thought was relevant:
echo '<form method="post">';
echo "<select onchange='this.form.submit()' id='chose_category' name = 'chose_category'>";
while($row = sqlsrv_fetch_array($hoofdrubrieken)){
echo '<option "value="' . $row['number'] . '">' . $row['name'] . '</option>';
}
echo '</select>';
echo'</form>';
In this case, the variable in $row['name'] is send as a POST variable, instead of $row['number']. I have checked this by printing the POST variable. Is there any way to send $row['number'] here, but still displaying $row['name']?
Change
while($row = sqlsrv_fetch_array($hoofdrubrieken)){
echo '<option "value="' . $row['number'] . '">' . $row['name'] . '</option>';
}
To:
while($row = sqlsrv_fetch_array($hoofdrubrieken)){
echo '<option value="' . $row['number'] . '">' . $row['name'] . '</option>';
}
Your current produces something like :
<select onchange='this.form.submit()' id='chose_category' name = 'chose_category'>
<option "value="number">name</option>
</select>
You need to remove the double-quote.
In a form after submitting the data, if i want to edit form the data in the form should be from database i have to echo the selected value from the database if data is present else it has to show the options to select the values from another table where these options are present
here is the code
please correct it
<select name="ccname" class="form-control form-color">
<?php
if(isset($storyData)) {
echo '<option value="' . $storyData['ccname'] . '">' . $storyData['ccname'] . '</option>';
}
else{
echo "<option value="">CCname</option>";
foreach(getNames() as $ccname)
echo '<option value="'.$ccname.'">'.$ccname.'</option>';
}
?>
</select>
after working with concatenation operator finally debugged the code
here is the code
that i corrected
<select name="ccname" class="form-control form-color">
``<?php if(isset($storyData))
{
echo "<option value = '" . $storyData['ccname'] . "'>".$storyData['ccname']."</option>";
}
else{
echo "<option value='1'>CCname</option>";
foreach(getNames() as $ccname)
echo '<option value="'.$ccname.'">'.$ccname.'
</option>';
}
?>
</select>
there is something wrong with the php post method, using php, the form code is the following:
<form method="post" action="keres.php">
<select name="kategoria_keres" id="kategoria">
<?php
$kategoria = mysqli_query($con,"SELECT distinct mufaj FROM adatok");
while ($row = mysqli_fetch_array($kategoria)) {
echo '<option value="' . $row['id'] . '">' . $row['mufaj'] . '</option>' ;
}
?>
</select>
<input type="submit" value="Keresés" id="keres" name="keres" />
</form>
The aim is to give the selected option's value using php $_POST method.
the processing php is the following:
<?php
include ("functions.php");
include ("connect.php");
$mufaj = $_POST['kategoria_keres'];
$result = mysqli_query($con,"SELECT * FROM adatok where mufaj='$mufaj'");
while ($row = mysqli_fetch_array($result)) {
echo '<tr>';
echo '<td>' . '' . $row ['cim'] . '' . '</td>';
echo '<td>' . $row['orszag'] . '</td>';
echo '<td>' . $row ['mufaj'] . '</td>';
echo '</tr>';
}
?>
When trying to print_r the $_POST it is empty, so this is the reason why the sql query returns with empty output.
The question is why does the $_POST not working, when the form passing it to the processing php?
please make sure your processing php file name is ="keres.php" because in the form action you set the keres.php.
I have a select value 'post', when someone wants to change the status of the post from unsolved to solved they have to choose a post en than click 'change to solved'
The problem is when I click on the button, it doesn't change because PHP takes not the whole title of the post. So when my post is called 'Photoshop crashes', it only sends 'Photoshop'. That's why it doesn't update in my database, the query can't find the right post, when the title of the post is only 1 word, my table gets updated.
<?php
if (isset($_POST['btnSolved']))
{
// wanneer er op de knop geklikt is proberen we user te saven in de databank
if (!empty($_POST['btnSolved']))
{
$solved = $_POST['unsolved'];
$conn = new mysqli("localhost","root","root","PhpProject");
if ($conn -> connect_errno) {
throw new Exception("No connection with database!");
} else {
$sql = "UPDATE Posts SET status = 'Solved' WHERE post='".$solved."'";
}
}
$conn->query($sql);
}
?>
In my body:
<h3>Change status</h3>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<?php
if(mysqli_num_rows($showBugs) > 0)
{
echo "<select name= unsolved>";
while ($row = mysqli_fetch_assoc($showBugs))
{
echo "<option value=" . $row['subject'] . ">" . $row['subject'] . "</option>";
}
echo "</select>";
}
?>
<br />
<input class="btn btn-info dropdown-toggle" type="submit" name="btnSolved" value="Change to solved" />
</form>
This is what I get when I do a print of the $sql
UPDATE Posts SET status = 'Solved' WHERE post='Photoshop'
Does someone know why PHP can post 1 word, but not the whole title? It might be something stupid, but I don't know how to fix this.
Your problem is a absence of the quotes in your html.
Fix this:
echo "<option value=" . $row['subject'] . ">" . $row['subject'] . "</option>";
to this:
echo '<option value="' $row['subject'] . '">' . $row['subject'] . "</option>";
Try enclosing the value in single quotes,
echo "<option value='" . $row['subject'] . "'>" . $row['subject'] . "</option>";
The browser won't parse this properly if $row['subject'] comprises two words:
echo "<option value=" . $row['subject'] . ">" . $row['subject'] . "</option>";
Your browser will read
<option value=two words>
as
<option value=two>
and not know what to do with words
use escaped double quotes
echo "<option value=\"" . $row['subject'] . "\">" . $row['subject'] . "</option>";
You need to add quotes for the value attribute. And if you have quotes in your value you need to pass $row['subject'] through htmlentities() like this:
echo '<option value="' . htmlentities($row['subject']) . '">' . htmlentities($row['subject']) . "</option>";
In this case htmlentities() for the label is not needed but it's good practice.