ok, so i have a select items in which the option comes from the database:
<select id="subject_code" name="subject_code">;
<?php
$stmt = $database->prepare('SELECT subject_code, subject_name FROM subject_schedule');
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$subj_code = $row['subject_code'];
$subj_name = $row['subject_name'];
echo "<option value=" . $subj_name . ">" . $subj_code . "</option>";
}
?>
</select>
an input box where the selected item should put its value:
<input id="subject_name" type="text" autocomplete="off" placeholder="Subject Name" name="time">
and a script where it will do the job:
$("#subject_code").on('change',function(){
//get selected option
var option = $(":selected",this);
//get its value
var value = option.val();
//get input box
var input = $("#subject_name")[0];
// set value and disable input
input.value = value;
});
the problem is whenever i select a value in which it has spaces on them, the input box only display the first word from the database.
example:
preferred output:
subject code: ENG111
subject name: Communication Arts 1
current output
subject code: ENG111
subject name: Communication
The problem is with quotes:
Replace this:
echo "<option value=" . $subj_name . ">" . $subj_code . "</option>";
With this:
echo "<option value='" . $subj_name . "'>" . $subj_code . "</option>";
jsFiddle
Related
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 want to make a search bar in a table called items with two different options: IID, Type. The code is below.
<h1> Search </h1>
<form name="search" action="items.php" method="get">
Search For: <input type="text" name="item"> in
<select name = "Option">
<option value= "IID"> IID </option>
<option value= "Type"> Type </option>
</select>
<input type="submit" value="search">
</form>
The data is stored as a table called Item with attributes IID and type.
I then do a query, but I'm getting lost here on how to change the select clause.
I have the code here so that it searches for item(s) with a certain type the user inputs, but how can I also change the clause so that it corresponds to the option menu in the search bar where you can find items with IID? Would I have to write 2 different queries?
Any help would be appreciated!
<?php
//SEARCH by type
$item = ucfirst($_GET["item"]);
if($item != null){
$result = executePlainSQL("select * from item where type = '" . $item . "'");
}
else{
$result = executePlainSQL("select * from item");
}
printItem_byType($result);
//Print result
function printItem_byType($result){
echo '<table>';
echo '<tr><td>IID</td><td>Type</td></tr>';
while ($row = OCI_Fetch_Array($result, OCI_BOTH)) {
echo "<tr><td>" . $row[0] . "</td>
<td>" . $row[1] . "</td>
<td><a href= profile.php?type= ". $row[2] . ">" $row[2] . "</td></tr>";
}
echo '</table>';
}
//SEARCH by type
$item = ucfirst($_GET["item"]);
$option = $_GET["option"];
if($item != null){
$result = executePlainSQL("select * from item where type = '" . $item . "' and filter_by = '" . $option . "'");
}
else{
$result = executePlainSQL("select * from item");
}
then add one colum called "filter_by" on the sql table.
I have this code but when I press the submit button No data is being transferred via Get, with the exemption on the submit.
<table cellspacing="0px">
<tr>
<td>Name</td><td>Today - <?php echo $date;?></td>
</tr>
<form method="get" action="update_reg.php">
<?php
$result = mysqli_query($con,"SELECT * FROM TABLE WHERE GROUP = 'Penguins' ORDER BY Rank, Name ");
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>";
if($row['Rank'] == "a"){
$rank = "TOP ";
}
if($row['Rank'] == "b"){
$rank = "MIDDLE ";
}
if($row['Rank'] == "c"){
$rank = "SECOND ";
}
if($row['Rank'] == "d"){
$rank = "BOTTOM ";
}
if($row['Rank'] == "e"){
$rank = "";
}
echo $rank . $row['Name'] . "</td>";
$num = $num + 1;
echo "<td><input type=\"text\" class=\"today\" id=\"" . $row['id'] . "\" data-number=\"" . $num . "\" size=\"1\" maxlength=\"1\"></td></tr>";
}
?>
</table>
<input type="submit" value="submit">
</form>
For some reason this isn't working, anyone got any ideas why? Thanks in advance.
You need to add a value="" attribute and name="" attribute to your <input>s.
For example:
echo "<td><input type=\"text\" class=\"today\" id=\"" . $row['id'] . "\" name=\"" . $row['id'] . "\" data-number=\"" . $num . "\" value=\"" . $num . "\" size=\"1\" maxlength=\"1\"></td></tr>";
I'm not sure what you're trying to submit exactly, but place that in the value for the value attribute and make sure to give each one a name attribute and value. In my example, I used $num for the value and $row['id'] for the name.
None of your <input> tags have name attributes. No name, no form submission.
GROUP is a reserved keyword.
So you need to backtick it as
`GROUP`
SELECT * FROM TABLE WHERE `GROUP` = 'Penguins' ORDER BY Rank, Name
UPDATE FROM LAST COMMENT
Input need a name which is not there and if you give same name for all of them they will not work. So give a name="something[]" and on submit get the data as array
I have the following code which provides a drop down list of all the rows in that specific table, this works fine. The code is below:
<?php
$con=mysqli_connect("localhost","user","pass","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT ID, NAME FROM b_sonet_group ORDER BY ID DESC");
echo "<select>";
echo "<option value=''>Select Your Project</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['ID'] . "'>" . $row['NAME'] . "</option>";
}
echo "</select>";
mysqli_close($con);
?>
I now want a second drop down list that is determined by what ever is selected in the one above based on the ID. So, I want something like:
$result2 = mysqli_query($con,"SELECT ID, ALBUM_NAME FROM a_different_table WHERE ID=ID_FROM_QUERY_ABOVE");
echo "<select>";
echo "<option value=''>Select Your Album</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['ID'] . "'>" . $row['ALBUM_NAME'] . "</option>";
}
echo "</select>";
mysqli_close($con);
?>
I basically want to get the ID from the first drop down to provide the results in the second dropdown. Can this be done?
You can't "only" do this with Ajax, but you should do it with Ajax.
PHP way (not suggested, and untested). Basically use isset and if it is, more will be added to the form. The POST from the select, is the select name. So change the plain select tag which I did in the example below. This also requires them to submit it.
$result = mysqli_query($con,"SELECT ID, NAME FROM b_sonet_group ORDER BY ID DESC");
echo '<form id="project_form" method="post">';
echo "<select id='select_your_project' name = 'select_your_project'>";
echo "<option value=''>Select Your Project</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['ID'] . "'>" . $row['NAME'] . "</option>";
}
echo "</select>";
if(isset($_POST['select_your_project'])){
$result2 = mysqli_query($con,"SELECT ID, ALBUM_NAME FROM a_different_table WHERE ID='".$_POST['select_your_project']."'");
echo "<select id='select_your_album' name = 'select_your_album'>";
echo "<option value=''>Select Your Album</option>";
while($row = mysqli_fetch_array($result2))
{
echo "<option value='" . $row['ID'] . "'>" . $row['ALBUM_NAME'] . "</option>";
}
echo "</select>";
}
echo '<input type="submit" value="Submit">';
echo '</form>';
if(isset($_POST['select_your_album'])){
//do form submitted stuff here
}
Ajax way (two separate files, untested but gives you the idea)
//Main page (view) START
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
//this will trigger automatically when they change the first select box
$('#select_your_project').on('change', function(event){
if($(this).val() == 'select_your_project'){
$("#ajax_reply_div").empty()
}else{
var values = $(this).serialize();
$.ajax({
url: "php_data_file.php",
type: "post",
data: values,
success: function(data){
$("#ajax_reply_div").empty().append(data);
},
error:function(){
$("#ajax_reply_div").empty().append('something went wrong');
}
});
}
});
</script>
<form id="id_of_form">
<?php
echo "<select id='select_your_project' name='select_your_project'>";
echo "<option value='select_your_project'>Select Your Project</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['ID'] . "'>" . $row['NAME'] . "</option>";
}
echo "</select>";
?>
</select>
<div id="ajax_reply_div">
</div>
<input type="submit" value="Submit">
</form>
//Main page (view) END
//php_data_file.php START
if(isset($_POST['select_your_project'])){
$result2 = mysqli_query($con,"SELECT ID, ALBUM_NAME FROM a_different_table WHERE ID='".$_POST['select_your_project']."'");
//as a note it is better to only send an array back then build the HTML with jQuery, but this way is easier if you are new to jQuery/Ajax
echo "<select id='select_your_album' name = 'select_your_album'>";
echo "<option value=''>Select Your Album</option>";
while($row = mysqli_fetch_array($result2)){
echo "<option value='" . $row['ID'] . "'>" . $row['ALBUM_NAME'] . "</option>";
}
echo "</select>";
}
//php_data_file.php END