I have a php page where user can select one category via dropdown list and after clicking 'go' button the relative images for that category will display on page below the dropdown list from database. This works fine. Now I want to display the images on page load for the 1st dropdown list category everytime then after based on user choice. I think my point is clear to all. for example, 1st category in the dropdown is Featured art, on loading the php page images related to Featured art always display on page. After that user can change it by changing the category from dropdown. Help me please.
Here is my complete code :
<body>
<?php
include ('connect-db.php');
?>
<form name="product" method="post" action="">
<table align="right" width="10%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Category</td>
<td>
<select name="category">
<?php
$sql = "SELECT id, art_name FROM category;";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
?>
<option value="<?= $row['id']; ?>"><?= $row['art_name']; ?></option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input name="go" type="submit" value="Go" /></td>
</tr>
</table>
</form>
<div align="center">
<ul class="display">
<?php
$id = (int)$_POST['category'];
$sql_search = "SELECT id, categoryid, path FROM list WHERE categoryid = $id";
$search = mysql_query($sql_search);
if (isset($_POST['go'])) {
while ($row = mysql_fetch_assoc($search)) {
?>
<li><img src="<?= $row['path']; ?>" border="0"></li>
<?php }
}
else {
}
?>
</ul>
</div>
</body>
Thanks in advance!:)
You may try to use GET request instead of POST. Using this you may check like:
<?php
$get_id = $_GET['category'];
if($get_id == null){
$get_id = <default-value-say-1>;
}
$id = (int)$get_id;
?>
Also, in the dropdown, set the default / first category selected.
When the user selects another option, redirect to url?category=<selected-id>
This is not a copy-paste solution, please take this as a starting
point and follow the best-practices for development in PHP or any PHP
framework of your choice.
Hope this helps!
Vivek
Related
So its been over 10 years since writing any code what so ever so i might be way off here. I have a table showing all students in a classroom populated by a foreach loop with an added box to input their grade. I need 1 submit button to insert one new row per student in the grades table in my database with their grade and the assignment name that is selected above.
I know i need to identify each grade value im just unsure how and it's only posting the person in the last row.
global $wpdb;
$asnames = $wpdb->get_results( "SELECT * FROM assignments" );
$results = $wpdb->get_results( "SELECT * FROM enrolled" );
?>
<div class="container"><center>
<form action ="<?php echo $_SERVER['REQUEST_URI']; ?>" method ="post">
<table class="form-table" id='topper'>
<tbody>
<tr>
<th>Assignment Name</th>
</tr>
<tr>
<td>
<select name="Assignment">
<?php foreach($asnames as $asname) { ?>
<option value="<?php echo $asname->id ?>"><?php echo $asname->assign_name ?></option>
<?php
} ?>
</select>
</td>
</tr>
</tbody>
</table></center>
</div>
<br><br>
<div class="container"><center>
<form action ="<?php echo $_SERVER['REQUEST_URI']; ?>" method ="post">
<table class="form-table" id='student'>
<tbody>
<tr>
<th>Students Name</th>
<th>Points</th>
</tr>
<?php foreach($results as $row){ ?>
<tr>
<td><?php echo "$row->student_name" ?></td>
<td><input type="text" class="regular-text" name="Grade"></td>
</tr>
<?php } ?>
<!-- </tbody> -->
</table>
<p><input type="submit" name="submitinsert" id="submitinsert" class="button button-primary" value="Submit"/></p> </form>
</center></div>
<?php
if ($_POST['submitinsert']){
$result = $wpdb->insert(
'grades',
array(
"student_name" => $row->student_name,
"assignment_id" => $asname->id ,
"grade" => $_POST['Grade']
)
);
}
I cant seem to get it to grab each row as it is in the table to insert, only the last row gets put in.
EDIT: I got the grade input worked out now just trying to get each loop put into the database with the single submit button.
this codes below came from my friend. i want to make a search under this codes . from this codes , it will display all the users to delete. what i want is i want to put search box and search button . so when i insert user id and press search only then it display which user that i want . where should i put this search text , button and php ? i want to put it under this else if codes . help me. im new :(
else if($_SESSION['jawatan'] == 'ADMIN') { ?>
<li> VIEW PROFILE </li>
<li> VIEW REQUEST </li>
<li>LOG OUT</li>
<li><img src="msc.jpg" width = "240" height ="80"></li>
<form method="get">
<input type="text" name="userid" placeholder="search" />
<inpu type="submit" value="Search" />
</form>
<h3>View User</h3>
<table border='1'>
<tr>
<td><b>#</b></td>
<td><b>Nama</b></td>
<td><b>Email</b></td>
<td><b>Division</b></td>
<td><b>Department</b></td>
<td><b>Delete</b></td>
</tr>
<?php
$i = 1;
$whr="";
if(isset($_GET['userid'])){
$whr.= " and mem_id='".$_GET['userid']."'"; // which field you want. If you want to do search name use LIKE instead =(equal to)
}
$result = mysql_query("SELECT * FROM members WHERE mem_role='USER' ".$whr);
if(mysql_num_rows($result)>0) {
while($row = mysql_fetch_array($result)) {
?>
<tr>
<td>
<?php echo $i; ?>
</td>
<td>
<?php echo $row['mem_name']; ?>
</td>
<td>
<?php echo $row['mem_email']; ?>
</td>
<td>
<?php echo $row['mem_division']; ?>
</td>
<td>
<?php echo $row['mem_department']; ?>
</td>
<td>Delete</td>
</tr>
<?php
$i++;
}
} else {
echo '<tr><td>No results found</td></tr>';
}
?>
</table>
<?php
}
HTML
<form method="get">
<input type="text" name="userid" placeholder="search" />
<inpu type="submit" value="Search" />
</form>
PHP
$whr="";
if(isset($_GET['userid'])){
$whr.= " and mem_id='".$_GET['userid']."'"; // which field you want. If you want to do search name use LIKE instead =(equal to)
}
MYSQL
$result = $result=mysql_query("SELECT * FROM members WHERE mem_role='USER' ".$whr);// add whr variable here
I can tell you the steps:
Create search text field and search button ( wrap in form if you want to do with php only)
on click of button ( that would be submit ), fetch that value from search text box and run query based on that
when results appear , fill the table data with that results using foreach.
hope it helps. Ask me for any issue.
I have a query to display all the records of the ebooks table from the database.
I displayed it in a table with a checkbox from the first column.
I'm trying to save the rows from the selected checkbox in the table width the Name of School but I don't have any idea.
I'm just a beginner in php, any help is appreciated. Here is my codes:
HTML codes:
$result1 = mysql_query("SELECT * FROM school GROUP BY SCHOOL_NAME");
while($row1=mysql_fetch_array($result1)) {
?>
<option><?php echo $row1['SCHOOL_NAME'];?></option>
<?php } ?>
</select>
<table class="table table-striped table-hover table-bordered datatable">
<thead style="background: #a83535; color: black;">
<tr>
<th style="width: 50px;"></th>
<th style="color: #3b3b3b;">Ebook Title</th>
<th style="color: #3b3b3b;">Category</th>
<th style="color: #3b3b3b;">File Name</th>
</tr>
</thead>
<tbody>
<?php
$result = mysql_query("SELECT * from ebooks ORDER BY EBOOK_TITLE");
$count = mysql_num_rows($result);
while($row=mysql_fetch_array($result)) {
?>
<tr>
<td>
<center><input type="checkbox" name="check[]" ></center>
</td>
<td><input type="" name="ebookname[]" value="<?php echo $row['EBOOK_TITLE'];?>"></label></td>
<td><input type="" name="ebookcategory[]" value="<?php echo $row['EBOOK_CATEGORY'];?>"></label></td>
<td><input type="label" name="ebookfilename[]" value="<?php echo $row['EBOOK_FILENAME'];?>"></label></td>
</tr>
<?php } //end of while loop ?>
</tbody>
</table>
<button type="submit" class="btn btn-hg btn-primary" name="AddEbooks">Submit</button>
I want to save the rows selected by the checkbox to database:
Table: school_management
Columns: ID_NUMBER, SCHOOL, EBOOK, FILE_NAME, DATE_ASSIGN
Values (from the HTML table to database): ('','$school_ebook','$ebookname','$ebookfilename','')
Here is the screenshot:
First, you will need to assign an ID number to the checkboxes so that you can differentiate each record. Using the ID_NUMBER would be ideal here. In your current script where you are displaying the checkbox, your line that reads this:
<center><input type="checkbox" name="check[]" ></center>
Should include the ID_NUMBER like this in the name. Additionally, a value should be assigned to the checkbox so we can verify later.
<center><input type="checkbox" name="check[<?php echo $row['ID_NUMBER'];?>]" value="1"></center>
Now you can go ahead and set up something that gets triggered when your form is submitted. You can essentially loop through each record and check it against the "check[ID_NUMBER]" checkbox to see if the value is 1. If the value is 1 then the record had been checked to save and you can then carry on saving the record.
How you want to handle the form is up to you - you could do AJAX to avoid reloading the entire page or you could do a simple form action="page_name_here.php" and handle the submission there. If you go about doing a simple form post without AJAX, it could be something like this -
Current page form tag:
<form name="" action="page_name_here.php" method="POST">
If you want to use the school_ebook var you need to assign the select options for the school_ebook a value:
<option value="<?php echo $row1['SCHOOL_NAME']'?>">
page_name_here.php:
<?php
/* Get the school name */
$school_ebook=$_POST["school_ebook"];
/* Query your books DB to get the records */
$result=mysql_query("SELECT * from ebooks ORDER BY EBOOK_TITLE");
$count=mysql_num_rows($result);
while($row=mysql_fetch_array($result)) {
$bookID=$row['ID_NUMBER'];
/* Get the Book ID and now we have to fetch from $_POST the value from the form */
if (array_key_exists($bookID, $_POST["check"])) {
$ischecked=$_POST["check"][$bookID];
/* See if this has a value of 1. If it does, it means it has been checked */
if ($ischecked==1) {
/* It is checked, so now in this area you can finish the code to retrieve the data from the row and save it however you like */
}
}
}
?>
Once completed with your inserts, etc., it should accomplish what you need.
I am creating a student login site for tutoring center. After a student logged in tutor can see student information and tutor also can submit student information along with tutor name. My problem is whatever i select the tutor name it always insert the last tutor name. I am using PDO for database connection. please can anybody give me an idea how can i solve this problem.Thanks..
Here is my html form code:
<form action="" method="post">
<table border="1">
<tr>
<th>Student Name</th>
<th>course</th>
<th>Tutor</th>
<th>Actions</th>
</tr>
<?php
//select all users from database
$rows = $database->selectFromOnline();
foreach($rows as $row){
$time = $row['time_out'];
?>
<tr>
<td> <?php echo $row['user_name'] ?></td>
<td> <?php echo $row['course'] ?> </td>
<td>
<select name='tutor'>
<?php
//select all instructor from instructor table
$sqls = $database->selectFromTutor();
foreach($sqls as $sql){
echo"<option value='$sql[tutor_ln]'> $sql[tutor_ln] </option>";
}
?>
</select>
</td>
<?php
echo" <td> <a href='tutor.php?user_name=$row[user_name]&&course=$row[course]&&tutor=$sql[tutor_ln]'>Delete</a></td>";
?>
</tr>
<?php
}
?>
</table>
</form>
This is my inserting code:
if(isset($_GET['user_name'])){
$user_name = $_GET['user_name'];
$course = $_GET['course'];
// if i use $_POST['tutor'] it gives me undefined variable error
$tutor = $_GET['tutor'];
//insert into report database table
$database->insetIntoReport($user_name, $course, $tutor);
//Redirect to current page
header('Location: tutor.php');
exit;
}
?>
You're outputting multiple <select name='tutor'> in your form. One select for EVERY time that inner ->selectFromTutor() is executed. As such, you'll get MULTIPLE instances of the tutor select, and only the LAST one will be submitted with the rest of the form.
Your form says <form action="" method="post"> you should be using $_POST variables in your code.
If you are using method="post" on your form, you should use $_POST[]
In addition to the other answers, you could also use $_REQUEST if do not want to bother, whether a form was sent via POST or GET.
I guess you are not willing to use post then you should write a javascript function like
<script type="text/javascript">
function DeleteTutor(name,course){
var tutor=document.getElementById('tutor').value;
window.location.href = 'tutor.php?username='+name+'&course='+course+'&tutor='+tutor;
}
</script>
capture name and course in variables $name and $course and your delete link modified to
<?php echo"<td> <a onclick=\"DeleteTutor('$name', '$course')\">Delete</a></td>"; ?>
Don't forget to add id to select for this to work
<select name='tutor' id="tutor">
All,
I've got the following page:
http://tinyurl.com/7lzr6qo
The original form code is:
<form id="dj_feedback" name="dj_feedback" action="../test-save-feedback" method="post">
<br>
<h2>How was the Event?</h2>
<hr>
<table><tr><td width="100px" valign="middle">
<b>Person<font color="#FF0000"> * </font></b>
</td><td valign="middle">
<select name="dj_name" id="dj_name">
<option value="original"></option>
<?php
$qrydj = "Select user_id, first_name, last_name from users where role='employee'";
$resultdj = mysql_query($qrydj);
while($resultsetdj = mysql_fetch_array($resultdj)){
?>
<option value="<?php echo $resultsetdj['user_id']; ?>"><?php echo $resultsetdj['first_name']." ".$resultsetdj['last_name']; ?></option>
<?php
}
?>
</select></td>
</tr>
</table>
<button class="submit" name="submit_feedback" value="submit_feedback">Get Paid!</button>
</form>
When I view the dropdown source it has a value on the select and then on my second page all I have is a simple echo statement to view what was passed like this:
<?php
session_start();
$user_id = $_POST['dj_name'];
echo "The user id is: ".$user_id;
?>
However, when I post this the value selected in the dropdown menu doesn't get passed. In other browsers this works perfectly fine however.
Does anyone have any idea why this isn't working properly?
Thanks!
You may need to make IE know the button element is in fact submitting the form - Perhaps edit your button:
<button type="submit" class="submit" name="submit_feedback" value="submit_feedback">Get Paid!</button>
Note that the type attribute is a valid one:
http://www.w3schools.com/tags/tag_button.asp
and html5:
http://www.w3schools.com/html5/tag_button.asp