How to get table column data when a button is clicked - php

Assume that I have a directory page that is generated by PHP. Each row data is from the database.
echo "<form action=\"reservation.php\" method=\"post\">";
for ($i=0; $i <$num_results; $i++) {
$row = $result->fetch_assoc();
if ( stripslashes($row['gymid']) == !0) {
echo "<tr>". PHP_EOL;
echo "<td>".stripslashes($i+1)."</td>". PHP_EOL;
echo "<td align=\"center\">".stripslashes($row['gymname'])."</td>". PHP_EOL;
echo "<td>".stripslashes($row['address'])."</td>". PHP_EOL;
echo "<td align=\"center\">".stripslashes($row['location'])."</td>". PHP_EOL;
echo "<td align=\"center\">".stripslashes($row['operatinghours'])."</td>". PHP_EOL;
echo "<td align=\"center\"><input type= \"submit\" id =\"gym".stripslashes($row['gymid'])."\" value=\"BOOK NOW\" class=\"bookbutton\" onClick=\"reply_click(this.id)\"</td>". PHP_EOL;
//echo "<td align=\"center\"><input type=\"submit\" name=\"gyminfo\" class=\"bookbutton\" value=\"".stripslashes($row['gymid'])."\" >BOOK NOW! </td>". PHP_EOL;
echo "</tr>". PHP_EOL;
}
}
echo "</form>";
So i have a "BOOK NOW" button for each row with unique id (gymxxxx). Upon clicking that button, the page will be redirect to reservation.php where there are a few dropdown boxes with gymname, location, timeslot and so on.
The following shows the dropdown box for gymname:
<label>Gym Name: </label>
<form id="gymInfoForm" method = "post" action = "reservation.php">
<select name="gymSelect"onchange="if (this.selectedIndex) formSubmit();">
<?php
$data = $_POST['gymSelect'];
echo "Data = " . $data;
$query = "SELECT * FROM gymname";
$result = $db->query($query);
$resultNum = $result->num_rows;
echo '<option value="NULL" >Please choose a gym</option>';
for($i = 0; $i < $resultNum; $i++)
{
$row = $result->fetch_assoc();
if($data == $row['gymname'])
{
echo '<option value="' . $row['gymname'] . '" selected>' . $row['gymname'] . '</option>'.PHP_EOL;
}
else
{
echo '<option value="' . $row['gymname'] . '" >' . $row['gymname'] . '</option>'.PHP_EOL;
}
}
?>
</select>
</div>
<div style="margin-bottom:10px">
My question is, how can i post the data (gymname and location) linked to the "BOOK NOW" button to the reservation.php such that the option for "gymname" and "location" will be preselected?
Thank you!

U can do <form> tag for each row or use JS witch map your data and send to the server.

It doesn't look like you have any 'names' for your inputs:
put:
name='bookNow'
in the book now button input tag.
Then put
name='gymName'
in your pre-populated gym name input tag.
Then you can get the values with php:
if(isset($_POST['bookNow'])){
$gymName = $_POST['gymName'];
}

Related

PHP MySQL keep selected option from dropdown menu when menu is select from database

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'];
}

How do I obtain the value of the option that was chosen in a select from?

In the PHP code below I have created a form that is used to create a drop down list.
<?php
echo "<body>";
echo "<div id='network_name' class='col-md-3'>";
echo "<h2> Agency Network </h2>";
echo "<form action='droplistpop.php' method='post'>";
echo "<select name='network'>";
while($result1 = mysqli_fetch_assoc($result)) {
unset($network_id, $network_name);
$network_id = $network_name['network_id'];
$network_name = $result1['network_code'];
echo '<option name="entry" value="' . $network_id . '">' . $network_name . '</option>';
$network_chosen = $network_id;
}
echo "</select>";
echo "<input name='submit' type='submit' value='Send' />";
echo "</form>";
echo "</div>";
?>
This code works as desired. It populates a drop down list based on the results of a database query. After the form has been submitted, I want to obtain the option that was selected and use it in another query. After the submit, I print the contents of the $_POST variable using:
print_r($_POST);
and my results are as follow:
Array ( [network] => [submit] => Send )
I want to get the value that was selected for network but it appears to be blank. Can anyone tell me what I'm doing wrong.
By the way, I am really new at coding and this is my first time using SO so please excuse any usage errors on my part. Thanks.
$network_id = $result1['network_id'];
instead of
$network_id = $network_name['network_id']
Output like this:
Array ( [network] => 2 [submit] => Send )
You are setting a wrong value to network_id, that's why it's output is blank. Also, the 'name' attribute on option tags is not needed. Try the changes below.
<?php
echo "<body>";
echo "<div id='network_name' class='col-md-3'>";
echo "<h2> Agency Network </h2>";
echo "<form action='droplistpop.php' method='post'>";
echo "<select name='network'>";
while($result1 = mysqli_fetch_assoc($result)) {
unset($network_id, $network_name);
$network_id = $result1['network_id'];
$network_name = $result1['network_code'];
echo '<option value="' . $network_id . '">' . $network_name . '</option>';
$network_chosen = $network_id;
}
echo "</select>";
echo "<input name='submit' type='submit' value='Send' />";
echo "</form>";
echo "</div>";
?>

Adding css to a form

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 ?

jquery .append() not working for my html

I have a program which appends an input(type="hidden") using jquery, to an html so that when I click the submit button, it passes the value to a php file and I can process it. However, it seems that the hidden type is not really being appended to the html nor it is being passed to the php file. I already used method="get" to see the values in the address bar and print_r to see the values being catched but there's nothing. To check if my form is actually passing a value, I added a
<input type="hidden" name="absent[]" value="testing" />
in the HTML and the value got passed but the ones in the jquery aren't.
Here are my files:
jquery:
$(function(){
$("td").click(function(){
if($(this).hasClass("on"))
{
alert("Already marked absent");
}
else
{
$(this).addClass("on");
var currentCellText = $(this).text();
var temp = $(this).attr('id');
$("#collect").append("<input type='hidden' name='absent[]' value = '" + temp + "'/>" + currentCellText);
alert(temp);
}
});
$("#clicky").click(function(){
$("td").removeClass("on");
$("#collect").text('');
$("#collect").append("Absentees: <br>")
alert(temp);
});
});
Here is the html part:
<?php
session_start();
include 'connectdb.php';
$classID = $_SESSION['csID'];
$classQry = "SELECT e.csID, c.subjCode, c.section, b.subj_name, e.studentID, CONCAT(s.lname, ', ' , s.fname)name
FROM ENROLLMENT e, CLASS_SCHEDULE c, STUDENT s, SUBJECT b
WHERE e.csID = c.csID
AND c.csID = '" . $classID . "'
AND c.subjCode = b.subjCode
AND e.studentID = s.studentID
ORDER BY e.sort;";
$doClassQry = mysql_query($classQry);
echo "<table id='tableone'>";
while($x = mysql_fetch_array($doClassQry))
{
$subject = $x['subj_name'];
$subjCode = $x['subjCode'];
$section = $x['section'];
$studentArr[] = $x['name'];
$studentID[] = $x['studentID'];
}
echo "<thead>";
echo "<tr><th colspan = 7>" . "This is your class: " . $subjCode . " " . $section . " : " . $subject . "</th></tr>";
echo "</thead>";
echo "<tbody>";
echo "<tr>";
for($i = 0; $i < mysql_num_rows($doClassQry); $i++)
{
if($i % 7 == 0)
{
echo "</tr><tr><td id = '". $studentID[$i] . " '>" . $studentArr[$i] . "</td>";
}
else
{
echo "<td id = '". $studentID[$i] . " '>" . $studentArr[$i] . "</td>";
}
}
echo "</tr>";
echo "</tbody>";
echo "</table>";
?>
Here's the html part with the form:
<form name="save" action="saveTest.php" method="post">
<div id="submitt">
<input type="hidden" name="absent[]" value="testing"/>
<input type="submit" value="submit"/>
</div>
</form>
And here's the php part which processes the form (saveTest.php):
<?php
$absent = $_POST['absent'];
//echo "absnt" . $absent[] . "<br>";
echo count($absent) . "<br>";
//print_r($_POST) . "<br>";
?>
Is the element with id collect inside the form? It needs to be.
$("<input type='hidden' name='absent[]' />").val(temp)
.appendTo('form[name="save"]');
$('#collect').append(currentCellText);
This works perfectly.
$("body").delegate("#editorjs_menu_list ul li a", "click", function(e){
e.preventDefault();
$(this).toggleClass("active");
});

Dependent dropdown PHP

Hello I want to show a table that have all the values from a mysql table but depending on the values that I will select from a dropdown menu. For example the dropdown list menu have a value named open. I just want to see the rows from the table that have that status. I will need to use Ajax for this?
Here is my code?
$status = $_POST['TipoStatus'];
echo ' Create Service ';
echo '</br>';
echo '</tr><tr><td><label for="TipoStatus"> Status:</label></td><td>';
$query = "SELECT TipoStatus FROM status"; // First Remar
$result = queryMysql($query);
if (!queryMysql($query)) {
echo "Query fail: $query<br />" .
mysql_error() . "<br /><br />";
}
else
{
echo '<select name = "TipoStatus" size = "1">'; // or name="toinsert[]"
// echo '<option value="none" selected="selected">None</option>';
while ($row_1 = mysql_fetch_array($result)) {
echo '<option value="' . htmlspecialchars($row_1['TipoStatus']) . ' selected="$row_1[9]" >' // Third remark
. htmlspecialchars($row_1['TipoStatus'])
. '</option>';
}
echo '</select>';
echo '</p>';
}
echo '<table border="1" >';
echo '<tr>';
echo '</br>';
echo '<th> Service ID</th>';
echo '<th>Title</th>';
echo '<th>Description</th>';
echo '<th>Notes</th>';
echo '<th>Submit By</th>';
echo '<th>Assigned Employee</th>';
echo '<th>Assigned Group</th>';
echo '<th>Category</th>';
echo '<th>Status</th>';
echo '<th>Urgency</th>';
echo '<th>Customer</th>';
echo '<th>Day Created</th>';
echo '</tr>';
$query = ("SELECT ServiceID, Title, Description, Notes, SubmitBy, AssignedEmp, " .
"AssignedGroup, NameCategory, TipoStatus, TiposUrgencia, CustomerName, DayCreation FROM Service where TipoStatus = '$status' ");
$result = queryMysql($query);
echo 'resultado' . mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td> '.$row['ServiceID'] .' </td>';
echo '<td>' .$row['Title']. ' </td>';
echo '<td>'.$row['Description'].'</td>';
echo '<td>'.$row['Notes'].'</td>';
echo '<td>'.$row['SubmitBy'].'</td>';
echo '<td>'.$row['AssignedEmp'].'</td>';
echo '<td>'.$row['AssignedGroup'].'</td>';
echo '<td>'.$row['NameCategory'].'</td>';
echo '<td>'.$row['TipoStatus'].'</td>';
echo '<td>'.$row['TiposUrgencia'].'</td>';
echo '<td>'.$row['CustomerName'].'</td>';
echo '<td>'.$row['DayCreation'].'</td>';
echo '</tr>';
}
mysqli_free_result($result);
echo $ticket_select;`enter code here`
echo '</table>';
echo '<form method = "post" action "rnseetickets.php">';
?>
Your code is a little meandering, and I can't quite tell if you're running this from the command line or as a web page, but it seems like you're wanting to filter a SQL result with a drop-down of options? That can be done with a simple form (if you don't mind a page refresh) or with AJAX (if you want it to filter without a page refresh):
// Run SQL
$sql = "SELECT TipoStatus FROM status";
if (!empty($_GET['status'])) {
$sql .= "WHERE `value`=\"".addslashes($_GET['status'])."\"";
}
$result = queryMysql($sql);
// Filter form
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method="get">";
echo "<select name=\"status\"><option>open</option><option>other option</option></select>";
echo "<input type=\"submit\" value=\"Filter\">";
echo "</form>";
// Table
// As you already have it
Greetings. You can submit a forum on the onchange event of the dropdown box.
Check this link:
javascript onchange submit
You can find more examples by searching 'javascript onchange submit'
Now when the user selects something in the drop down box the form will submit and you can use your php code do filter the results.

Categories