This short program is suppose find the column names of a table X, and create a form with at least one text field and one select element that contains all the names of the columns of the table. With that information, the user can perform a search on this table and further specify on which column he would like to do the search. I would like it for the user to be able to add more text fields with matching select elements, just in case he wants to refine his search.
How can I dynamically add those extra fields when ever the user press a button?
<?php
$table_name = "tablename";
mysql_connect("localhost", "root", "");
$query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '$table_name'";
$result = mysql_query($query);
$column_names="";
while($row = mysql_fetch_array($result)){
$column_names == "" ? $column_names .= $row["COLUMN_NAME"] : $column_names .= "," . $row["COLUMN_NAME"] ;
}
$column_names = explode(",", $column_names);
?>
<html>
<head>
<title>SQLSEARCH</title>
</head>
<body>
<form action="index.php" method="post">
<?php
echo "Search: <input tpe=\"text\" name=\"advtext[]\" /> in ";
echo "<select name=\"advselect[]\">";
foreach($column_names as $value)
echo "<option>" . $value . "</option>";
echo "</select>";
?>
<input type="hidden" name="searchsent" value="1" />
<input type="submit" name="searchbutton" value="Search" />
</form>
<input type="button" name="addattributes" value="Add Search Attributes" / onclick="AddSelect();">
</body>
</html>
This function adds an input element and a select element, every time the user presses the button.
function AddSelect(){
var newInput = document.createElement('input');
newInput.type='text';
newInput.name = 'advtext[]';
<?php
foreach($column_names as $value => $i){
echo "\tvar newOption" . $value . "=document.createElement('option')" . "\n";
echo "\tnewOption" . $value . ".value='" . $i . "';" . "\n";
echo "\tnewOption" . $value . ".innerHTML='" . $i . "';" . "\n\n";
}
?>
var newSelect = document.createElement('select');
newSelect.name = 'advselect[]';
<?php
foreach($column_names as $value => $i){
echo "\tnewSelect.appendChild(newOption" . $value . ")" . "\n";
}
?>
var SubmitButton = document.forms.myform.searchbutton;
document.forms.myform.insertBefore(newInput, SubmitButton);
document.forms.myform.insertBefore(document.createTextNode(" in "), SubmitButton);
document.forms.myform.insertBefore(newSelect, SubmitButton);
}
You can use innerHTML properties for your form . Otherwise use Ajax Functionality to add the extra text box to add dynamically.
Related
this is a code that generates a multiple dropdown list from a MySQL database and creates a table after clicking on a submit button with a selected option by default.
I need the table to be created when the page is loaded and also the dropdown list to keep the options selected after clicking the button.
Thanks in advance.
<?php
include 'conBBDD_1.php';
$db_handle = new conBBDD();
$TourResult = $db_handle->runQuery("SELECT DISTINCT bbcTour FROM concerts ORDER BY bbcDate DESC");
?>
<form method="POST" name="Search" action="example.php">
<?php
if (! empty($TourResult)) {
$format = "Tour 2020";
$select = 'selected';
echo "<select name='tour[]' id='bb_tour' multiple='multiple'>";
foreach ($TourResult as $key => $value) {
echo "<option value='" . $TourResult[$key]['bbcTour']."'";
if($TourResult[$key]['bbcTour']==$format) // to select one option only
echo $select. ">";
else
echo ">";
echo $TourResult[$key]['bbcTour'].'</option>' . "\n";
}
}
echo "</select>";
?>
<input type="submit" id="button" value="OK">
</form>
I don't quite understand the issue, but have cleaned this up for you.
If the issue is that (like your comment) you just want one item to be selected, remove the 'multiple' attribute from the select tag here.
Otherwise the 'multiple' tag and 'selected' just need to be lone strings w/ values.
<?php
include 'conBBDD_1.php';
$db_handle = new conBBDD();
$TourResult = $db_handle->runQuery("SELECT DISTINCT bbcTour FROM concerts ORDER BY bbcDate DESC");
echo '<form method="POST" name="Search" action="example.php">';
echo "<select name='tour[]' id='bb_tour' multiple>";
if (!empty($TourResults)) {
$format = "Tour 2020";
foreach ($TourResults as $Tour) {
echo "<option value='" . $Tour . "'";
if($Tour === $format) {
echo ' selected';
}
}
echo ">" . $Tour . "</option>\n";
}
echo "</select>";
echo "</form>";
?>
In command line, sqlite correctly displays the accented characters contained in my database.
In PHP, echo shows me correctly as well.
But when I do a query from my PHP page, it does not display correctly. I can not find where the encoding problem is.
Thank you in advance for your help.
<?php
$db = new SQLite3('database.sqlite');
if(isset($_GET['search'])) {
$rec = htmlentities($_GET['search']);
} else {
$rec = '';
}
$result3 = $db->query('SELECT lastname,firstname,phone,location FROM rh WHERE name != "" AND lastname || " " || firstname || " " || location LIKE "%'.$rec.'%" ORDER BY name asc LIMIT 25');
?>
The HTML form :
<form action="" method="get">
<input type="search" name="search" value="<?php echo $rec; ?>" /> <input type="submit" value="Search" />
</form>
And the results on another php page :
<?php
echo '<h3>Results</h3>';
echo '<tr><th>Name</th><th>Phone</th><th>Location</th></tr>';
while ($row = $result3->fetchArray(SQLITE3_ASSOC)) {
echo '<tr><td>' . $row['lastname'] . ' ' . $row['firstname'] . '</td><td>' . $row['phone'] . '</td><td>' . $row['location'] . '</td></tr>';
}
$db->close();
?>
Maybe you need to specify the propper encoding in your HTML-Head e.g.:
<html>
<head>
<meta charset="utf-8">
...
</head>
And/or you need to de/encode your data before output utf8_encode();.
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 ?
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
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.