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)
Related
I have a problem with my page. I was trying to solve it by a lot of tutorials but i don´t know how to make it work. Simply put i have a database of objects. When i select object, page will redirect to another where are shown all informations about the object. But i need to keep the selected option in drop down menu. There is 110 objects so if i select object number 25, informations will show but the drop down menu wont stay on number 25. Can somebody help me with it?
<form action="dbobj2.php" method="post" name="form1">
Zoznam objektov<br>
<?php
include('System/connect.php');
$sql = "SELECT Objekt FROM DBObj";
$result = mysqli_query($db,$sql);
echo "<select name='Objekt'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['Objekt'] . "'>" . $row['Objekt'] . "</option>";
}
echo "</select>";
?>
<input name="btnSubmit" type="submit" value="Vyber">
</form>
<?php
echo $_POST['Objekt'];
echo "<hr>";
$strSQL = "SELECT * FROM DBObj WHERE Objekt = '".$_POST['Objekt']."' ";
$objQuery = mysqli_query($db,$strSQL);
$objResult = mysqli_fetch_array($objQuery);
$imgRes=$objResult['URLobr'];
echo '<img src="http://page.sk.sk/imgs/'.$imgRes.'" alt="obj" height="600" width="800"/>';
echo "<hr>";
echo $objResult['Text'];
?>
First page php
<form action="dbobj2.php" method="post" name="form1">
Zoznam objektov<br>
Vyberte si zvolený objekt z menu a stlačte tlačidlo výber<br>
<?php
include('System/connect.php');
$sql = "SELECT Objekt FROM DBObj";
$result = mysqli_query($db,$sql);
echo "<select name='Objekt'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['Objekt'] . "'>" . $row['Objekt'] . "</option>";
}
echo "</select>";
?>
Check whether the post value matches the object id in the loop.
$selcted = $_POST['Objekt'] == $row['Objekt'] ? ' selected' : '';
If it does $selected is set to " selected" and added to the option.
echo "<option value='" . $row['Objekt'] . "'" . $selected . ">" . $row['Objekt'] . "</option>";
You can send that value $_POST['Objket'] via a variable let us say $selected_value and in the option tag you can write
<option value='" .$row['Objekt']. "' '.if($row['Objket']==$selected_value) echo selected.'>" .$row['Objekt']. "</option>
When you select a option and click on button then it will work otherwise no.
if(isSet($_POST['Objekt']))
{
echo $_POST['Objekt'];
echo "<hr>";
$strSQL = "SELECT * FROM DBObj WHERE Objekt = '".$_POST['Objekt']."' ";
$objQuery = mysqli_query($db,$strSQL);
$objResult = mysqli_fetch_array($objQuery);
$imgRes=$objResult['URLobr'];
echo '<img src="http://page.sk.sk/imgs/'.$imgRes.'" alt="obj" height="600" width="800"/>';
echo "<hr>";
echo $objResult['Text'];
}
I am trying to add CSS to my form but not sure how to do this. The form is created in php and MySQL, in browser it looks like: http://gyazo.com/5d099ead9bd6ea83859a5114b2438748
I need to allign the text and drop downs so they are in the equal throughout and add some spacing. Anyone help with CSS for this?
html currently:
<div class="wrap">
<img src="/images/logo.png" alt="Highdown logo" />
<h1>Entry form</h1>
</div>
css currently:
.wrap {
position: relative;
}
The form is produced with this:
if ($event_result = $con->query("SELECT Event.Name FROM event")) {
echo "<form method =\"POST\" action=\"save.php\"> ";
while ($row = $event_result->fetch_assoc()) {
echo $row['Name']. ' ';
if ($student_result = $con->query("SELECT Student.Form, Teacher.Form, Student.Forename, Student.Surname, Student_ID " .
"FROM Student, Teacher " .
"WHERE Student.Form = Teacher.Form AND Teacher.Username = '" . $_SESSION['Username'] . "'")) {
if ($student_result->num_rows) {
echo "<select name ='". $row['Name']."'>";
while ($row1 = $student_result->fetch_assoc()) {
echo '<option value="" style="display:none;"></option>';
echo "<option value ='" . $row1['Student_ID'] . "'>" . $row1['Forename'] . ' ' . $row1['Surname'] . "</option>";
}
echo "</select> <br />";
}
}
}
echo '<input type="submit" value ="Submit">';
echo '<input type="reset" value ="Reset">';
echo '<input type="button" value = "Add student" onclick="location.href=\'http://localhost/sportsday/addstudent.php\'">';
echo '<input type="button" value = "Delete student">';
echo "</form>";
}
Use
<form>
<table>
<tr> //1st Table row
<td></td> //Table column data
<td></td> //table column data
</tr> //1st row ends
<tr> // 2nd Table row
<td></td> //Table column data
<td></td> //table column data
</tr> //2nd row ends
</table>
</form>
This will give you a better layout of the form.
This should work i did not try as i dont have the database
//Query to display all events
if ($event_result = $con->query("SELECT Event.Name FROM event")) {
echo "<form method =\"POST\" action=\"save.php\"> ";
echo '<table>';
echo '<tr>';
echo '<td>';
while ($row = $event_result->fetch_assoc()) {
echo $row['Name']. ' ';
echo '</td>';
if ($student_result = $con->query("SELECT Student.Form, Teacher.Form, Student.Forename, Student.Surname, Student_ID " .
"FROM Student, Teacher " .
"WHERE Student.Form = Teacher.Form AND Teacher.Username = '" . $_SESSION['Username'] . "'")) {
if ($student_result->num_rows) {
echo '<td>';
echo "<select name ='". $row['Name']."'>";
while ($row1 = $student_result->fetch_assoc()) {
echo "<option value ='" . $row1['Student_ID'] . "'>" . $row1['Forename'] . ' ' . $row1['Surname'] . "</option>";
}
echo "</select> <br />";
echo '</td>';
echo '</tr>';
}
}
}
echo '</table>';
echo '<input type="submit" value ="Submit">';
echo '<input type="reset" value ="Reset">';
echo '<input type="button" value = "Add student" onclick="location.href=\'http://localhost/sportsday/addstudent.php\'">';
echo '<input type="button" value = "Delete student">';
echo "</form>";
}
?>
you can directly write in css
form {
⋮ declare css
}
or give name to form
form[name="value"]{
⋮ declare css
}
or add any class or id on form
#formid{
⋮ declare css
}
.formclass{
⋮ declare css
}
First , check your database...
May be there is Another Issue not related to Tabular Output.
So , First remove Table Tag..and check whether its working ?
Then try in HTML TABLE TAG
Otherwise give me sample database .sql File and complete PHP code in google drive or on shared drive.
So that I can check and identify where is problem ?
I created a drop down list that has been populated by the database and now I'm having trouble retrieving the data. Normally, I would know how to retrieve the value of the drop down list if I had to manually name the data, but in this case, I'm not quite sure how I would name it.
Here is my current code:
<h1>Generate Reports</h1>
<form enctype="multipart/form-data" action="http://localhost/yiiFolder/index.php/create" method="post">
<table>
<tr>
<td><strong>Materials</strong></td>
<?php
mysql_connect('host', 'root', 'password');
mysql_select_db ("db");
$sql = "SELECT material_name FROM materials";
$result = mysql_query($sql);
echo "<td><select name='materials'>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['material_name'] . "'>" .
$row['material_name'] . "</option>";
}
echo "</select></td></tr> ";
$sql2 = "SELECT location_name From locations";
$result2 = mysql_query($sql2);
?>
<td><strong>Locations</strong></td>
<?php
echo "<td><select name='locations'>";
while ($row2 = mysql_fetch_array($result2))
{
echo "<option value='" . $row2['location_name'] . "'>" .
$row2['location_name'] . "</option>";
}
echo "</select></td></tr>";
?>
<tr>
<td><button name="submit" type=submit>Generate</button></td>
</tr>
</table>
</form>
<?php
$material = $row['material_name'];
$locations = $row2['location_name'];
$generate = $_POST['submit'];
if(isset($generate))
{
echo $material;
echo $locations;
}
?>
You're trying to capture value before the submit button is hit. Also, as Hanky pointed out you're using the wrong names while referring to select data. You should do this instead
if(isset($_POST['submit'])) // this code will run after the button is clicked
{
$material = $_POST['materials']; // and not material_name
$locations = $_POST['locations']; // and not location_name
echo $material;
echo $locations;
}
PS: You're following a very unsecure way of developing a web application. At the very least you need to switch to PDO and always escape the data.
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
I have two dropdown boxes and I want the user to choose values from both.
The dropdown boxes are filled with results of a query on page load. I want these values to then be stored as a session variable for use in subsequent queries.
First file = choose.php
<html>
<form action="choose2.php" method="post">
<?//db connection stuff taken out from here
}
// fill classes
$result = mysqli_query($con,"SELECT * FROM classes");
echo "<select id='classlist'>";
while($row = mysqli_fetch_array($result))
{
echo " <option value=" . $row['id'] . ">" . $row['classname'] . "</option>";
}
echo "</select>";
//fill schemes of work
$result = mysqli_query($con,"SELECT * FROM schemes_of_work");
echo "<select id='sowlist'>";
while($row = mysqli_fetch_array($result))
{
echo " <option value=" . $row['id'] . ">" . $row['name'] . "</option>";
}
echo "</select>";
mysqli_close($con);
?>
<input type="submit">
</form>
</html>
second file = choose2.php
<?php session_start();
$_SESSION['classname'] = $_POST["classlist"];
$_SESSION['sowname'] = $_POST["sowlist"];
?>
<?php echo $_SESSION['classname'];?>
<?php echo $_SESSION['sowname'];?>
I cant get this to work though - I am getting an empty page on choose2 and the following error in apache log:
"Undefined index: sowlist in /var/www/assessment/choose2.php on line 3,
The select tag should have a name attribute to be able to use it in php POST.
Try
<select id='classlist' name="classlist">
The same for the other one.