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>
Related
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)
i have a search form whose action attribute contains an id to be passed in the URL and form is submitted from get method for user to get the search results to be shared or bookmarked.
Now the problem i am facing is, that id i have passed in action is not displaying in the URL but all other input field's values are appearing fine.
This is my form:
<form action="trails.php?pname=<?php echo $getName;?>" id="filter" method="GET" name="filter">
<select class="multiselect form-control" multiple="multiple" name='activity[]' id="activity">
<?php
$toaSql = mysql_query("select * from type_of_activity where cat_id='1'");
while ($toaRow = mysql_fetch_array($toaSql)) {
echo "<option value='" . $toaRow['typeOfActivity'] . "'><img style='width: 21px; margin-left: 5px;' src='home/images/" . $toaRow['catImg'] . "'> " . $toaRow['typeOfActivity'] . "</option>";
}
?>
</select>
<select class="multiselect form-control" multiple="multiple" name='activity[]' id="activity">
<?php
$toaSql = mysql_query("select * from type_of_activity where cat_id='2'");
while ($toaRow = mysql_fetch_array($toaSql)) {
echo "<option value='" . $toaRow['typeOfActivity'] . "'><img style='width: 21px; margin-left: 5px;' src='home/images/" . $toaRow['catImg'] . "'> " . $toaRow['typeOfActivity'] . "</option>";
}
?>
</select>
<select class="multiselect form-control" multiple="multiple" name='activity[]' id="activity">
<?php
$toaSql = mysql_query("select * from type_of_activity where cat_id='3'");
while ($toaRow = mysql_fetch_array($toaSql)) {
echo "<option value='" . $toaRow['typeOfActivity'] . "'><img style='width: 21px; margin-left: 5px;' src='home/images/" . $toaRow['catImg'] . "'> " . $toaRow['typeOfActivity'] . "</option>";
}
?>
</select>
</form>
trail.php page:
if(isset($_GET['pname'])){
$getName = $_GET['pname'];
// print_R($getName);
if($getName=='video'){
$queryVideo = "SELECT * FROM `contributevideo` WHERE status='active' " ;
//echo $queryVideo;
}elseif($getName=='album'){
$queryAlbum = "SELECT * FROM `contributeimage` WHERE status='active' AND category='album'" ;
}
}
Trails.php page run the sql queries according to the page name it gets from the url, but in url its not finding any pname variable so the queries are not executing.
Where i am doing wrong please guide.
Thanks
Why are you describing parameter as URL-encoded?
<form action="trails.php?pname=<?php echo $getName;?>" id="filter" method="GET" name="filter">
// Existing form
Since your form's method is 'GET', you can just describe a hidden input.
<form action="trails.php">
<input type="hidden" name="pname" value="<?php echo $getName; ?>"/>
// Existing form
It should work.
The issue with your code is that you set the form to submit the values using the GET method. This means that the values from the form fields will be passed in the URL, overwriting (removing) your pname value.
To fix it just change the method of the form to POST and the PHP code to access activities through $_POST['activities'], it should work then.
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.
I have a form embedded in table that is created on the initial page load:
echo "<form id='showbooking' action='calendar.php' method='post'>";
echo "<td name='clickedcalid' value='" . $resarr[$k]['calid'];
echo "' title='" . $resarr[$k]['coms'] . "' class='hols'>";
echo $resarr[$k]['typ'] . "</td></form>";
I submit the form when a cell in the table is clicked and want to pass the value of the cell that was clicked.
<script>
$(".hols").click(function() {
$("#showbooking").submit();
})
</script>
After the reload I am testing with this:
echo "<p> ID of clicked booking was: " . $_POST['clickedcalid']; . "</p>";
but the value is not being passed and I'm not sure why.
Possibly because the form is embedded in a table?
Firstly, the form element needs to go inside the td. Secondly, only the values of form elements are sent in a form submission, ie input, select, textarea etc. Try this:
echo '<td class="hols" title="' . $resarr[$k]['coms'] . '">';
echo '<form id="showbooking" action="calendar.php" method="post">';
echo '<input type="text" name="clickedcalid" value="' . $resarr[$k]['calid'] . '" />';
echo $resarr[$k]['typ'] . '</form></td>';
You can't put <form> inside a tr element, wrapping td. <tr> specification says
Permitted content: Zero or more <td> or <th> elements, or a mix of them
Also value attribute is not valid for td element. You need to restructure your markup.
echo '<td class="hols" title="' . $resarr[$k]['coms'] . '">' .
'<form id="showbooking" action="calendar.php" method="post">' .
'<input type="hidden" name="clickedcalid" value="' . $resarr[$k]['calid'] . '">' .
$resarr[$k]['typ'] .
'</form>' .
'</td>';
To get value from $_POST you need to have input tag
echo "<form id='showbooking' action='calendar.php' method='post'>";
echo "<td><input type='text' name='clickedcalid' value='" . $resarr[$k]['calid'];
echo "' title='" . $resarr[$k]['coms'] . "' class='hols' />";
echo $resarr[$k]['typ'] . "</td></form>";
Thanks,
Naumche
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.