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.
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'm having an issue where, when I save a variable in a session array, it only stores the first word. That is, if it's 'Company one', it would only save 'Company'. The variable comes from a selection list:
function listCompany() {
include 'includes/connection.php';
$stmt = $conn->prepare("SELECT CompanyName FROM Portal.company ORDER BY CompanyName ASC");
$stmt->execute();
$stmt->bind_result($col1);
?>
<select name="CompanyName">
<?
$blank = "";
echo "<option value=" . $blank . "> </option>";
while ($stmt->fetch()) {
echo "<option value=" . $col1 . ">" . $col1 . "</option>";
}
?>
</select>
<?
}
Then stored in a session:
$_SESSION['NewOrder'] = $array2 = array(
"CompanyName" => $_POST['CompanyName'],
When I echo the variable, I just get the first word. Any ideas what I'm doing wrong?
You are missing the quotes around the value. It should be
echo "<option value=\"$blank\"> </option>";
while ($stmt->fetch()) {
echo "<option value=\"$col1\">$col1</option>";
}
In your code the HTML output will be
<option value=Company one>Company one</option>
^-----^ <-- this part is taken as value the rest
after space is invalid HTML so gets ignored
It should be
<option value="Company one">Company one</option>
The problem is because of this line,
echo "<option value=" . $col1 . ">" . $col1 . "</option>";
You didn't quote your value in single quotes. It should be,
echo "<option value='" . $col1 . "'>" . $col1 . "</option>";
When I load my page, the value of the variable, $v_UpdateONE, is "Select Version". When I select a version, the value goes blank.
I need to grab the selected value for use in a DB update statement.
Thank you for any assistance. -James
<FORM METHOD="post" ACTION="Update.php" WIDTH="50">
<?php
$avQuery = "SELECT $v_software1 FROM version_master.vermas_availableversions WHERE $v_software1 IS NOT NULL ORDER BY SortCol DESC";
$a_AvailVers = mysql_query($avQuery);
#_Version dropdown box
echo "<select NAME='AvailVersONE' ONCHANGE=submit()>";
echo "<option>Select Version</option>";
while ($row = mysql_fetch_array($a_AvailVers)) {
echo "<option value='" . $row['$v_software1'] . "'>" . $row[$v_software1] . "</option>";
}
echo "</select>";
$v_UpdateONE = $_POST['AvailVersONE'];
echo $v_UpdateONE;
?>
</FORM>
You have an error in
value='" . $row['$v_software1'] . "'
Since $v_software1 is in single quotes, it will be literal $v_software1.
Try removing the quotes -
value='" . $row[$v_software1] . "'
You need to post before you can read $_POST data.
Form File
<FORM METHOD="post" ACTION="Update.php" WIDTH="50">
<?php
$avQuery = "SELECT $v_software1 FROM version_master.vermas_availableversions WHERE $v_software1 IS NOT NULL ORDER BY SortCol DESC";
$a_AvailVers = mysql_query($avQuery);
#_Version dropdown box
echo "<select NAME='AvailVersONE' id='AvailVersONE' ONCHANGE=submit()>";
echo "<option>Select Version</option>";
while ($row = mysql_fetch_array($a_AvailVers)) {
echo "<option value='" . $row['$v_software1'] . "'>" . $row[$v_software1] . "</option>";
}
echo "</select>";
?>
<button type="submit"> <!-- this will draw a submit button -->
</FORM>
then on your Update.php
<?php
$v_UpdateONE = $_POST['AvailVersONE'];
echo $v_UpdateONE;
?>
Sometimes, the ID needs to be filled up (browser dependent)
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'm trying to create a table that shows the time and the task a user has to do.
All this is saved in a database. For each row and column there is a selectbox(Usernames) and an <input>(for comments).
Here is a screenshot: http://snag.gy/NYwsJ.jpg
The name of each selectbox is the id of the time and task
echo "<select name='" . $x['idTask'] . "-" . $y['idTime'] . "'>
(e.g: name="1_23")
The problem is when I load the page it is supposed to display all the records that are already saved in the database.
For the <select> I tried using this an if:
if ($x["fiUser"] == $z["idUser"]){echo "selected";}
And here is the table:
<?php
echo"<div class='dv_Table'><table><tr><th></th>";
foreach (SelectTime() as $r) {
echo "<th>" . $r['dtTime'] . "</th>";
}
echo "</tr>";
echo "<form method='post'>";
$userselect = "";
foreach (SelectCalTask() as $x) {
echo "<tr><td>" . $x['dtTask'] . "</td>";
foreach (SelectTime() as $y) {
echo "<td>";
echo "<select name='" . $x['idTask'] . "-" . $y['idTime'] . "'>
<option value='0'> -- None -- </option> ";
foreach (SelectUser_Name() as $z) {
echo "<option value='" . $z["idUser"] . "'";
if ($x["fiUser"] == $z["idUser"]) {
echo "selected";
} echo" >" . $z["dtFirstName"] . " " . $z["dtLastName"] . "</option> ";
}
echo " </select>
<input type='text' name='" . $x['idTask'] . "_" . $y['idTime'] . "' value='" . "_" . $y['idTime'] . "'></td>";
}
echo "</tr>";
}
echo "</table></div>";
echo "<input type='submit' name='updateCalendar' value='Update'></form>";
?>
Better, would be if could send an Update query right after the "onchange" or right after finishing the comment in the input.
Is that possible ?
Thank you in advance
*
Better, would be if could send an Update query right after the
"onchange" or right after finishing the comment in the input. Is that
possible ?
*
Why not?
$('.yourValue').on('change', function (e) {
$.ajax({
type: "POST",
url: "/path-to-update-query",
success: function (data) {
}
});
});