Page 1 of 1
How should i go about doing this? (again, a bit newbish)
Posted: Wed Jul 27, 2005 1:55 pm
by Fimble
I'm writing a system for searching my mySQL database...
search.php shows the user a form where he can search the student table by student number, surname or course attended.
the user then clicks a "Search" button and is taken to search_studens.php where the database is searched based on the given input.
A table is shown with the results along with 2 buttons "Search" and "Delete".
I want the user to be taken back to search.php when "Search" is clicked and when "Delete" is clicked the records returned by the search shold be removed.
My problem is the only way i've been performing php actions up to now is with somthing like
Code: Select all
<form action="search_student.php" method="POST">
which loads up the declared php page when the forms submit button is pressed passing along variables via $_POST.... but i dont want the user to leave or be taken to another page unless he hits "Search" (in which case he retuns to search.php).
Is there a way to link a php function to the button press without going to another PHP page... but if i have to how do i pass on the info about the search field and content?
thanks.. hope that makes sense.
Posted: Wed Jul 27, 2005 2:12 pm
by nielsene
WHy not use a simple link, instead of a button, for the "return to search" type use?
Or you can do something like
Code: Select all
<?php
if (isset($_POST["submit"])) {
if ($_POST["submit"]=="search") {
relativeRedirect("search_students.php"); // see code snippets
exit;
}
if ($_POST["submit"]=="delete") {
// do your delete stuff
}
}
?>
Posted: Wed Jul 27, 2005 2:19 pm
by Fimble
i dont think the link will look right next to a button.
basicaly after a search the user has two choices: search again or delete. I want to keep the interface simple and use the same control for search again as for delete so the user can see they are his two viable movments from this point in the process and not get confused.
Where would i put that code you have shown me? in the php page that shows the search results or in a new php page that the formaction would goto after a button was pressed?
If its the latter how should i pass on the information to be deleted? to page doing the deletion?
thanks and sorry to seem so thick.
Posted: Wed Jul 27, 2005 2:26 pm
by nielsene
Fimble wrote:i dont think the link will look right next to a button.
basicaly after a search the user has two choices: search again or delete. I want to keep the interface simple and use the same control for search again as for delete so the user can see they are his two viable movments from this point in the process and not get confused.
Good answer, just wanted to make sure you were aware of the option.
Where would i put that code you have shown me? in the php page that shows the search results or in a new php page that the formaction would goto after a button was pressed?
If its the latter how should i pass on the information to be deleted? to page doing the deletion?
This could would go in the page that is the "action" of the form encasing those two buttons. As written it would belong more on a "delete_entries.php" style page, so I would possibly change the action to that and make the new page.
Alternatively you could just have two forms:
Code: Select all
<form action="e;search_students.php"e; method="e;post"e;>
<input type="e;submit"e; name="e;submit"e; value="e;Search Again"e; />
</form>
<form action="e;delete_students.php"e; method="e;post"e;>
<input type="e;submit"e; name="e;submit"e; value="e;Delete Results"e; />
</form>
Posted: Wed Jul 27, 2005 2:47 pm
by Fimble
nice one, cheers

Posted: Wed Jul 27, 2005 3:02 pm
by Fimble
Ok going with the 2 forms idea how do i pass the users
(1) Choice of search field (course, id or surname as chosen via radio button)
&
(2) input
so i can do a: DELETE FROM students WHERE (1) = (2);
in the delete_students.php
thanks again.
I ask as those choices where made 2 pages ago.. like so
search.php: The user selects the search field(1) and data to be searched for (2) -->
search_students.php: the results are displayed and the option to search again or delet are offered. -->
delete_student.php: some how pass on 1 & 2 so i can do: DELETE FROM students WHERE (1) = (2);
Posted: Wed Jul 27, 2005 3:15 pm
by nielsene
The "quick and insecure" way would be to set them in hidden form elements. The better way would be to stick them into the $_SESSION array.
Posted: Wed Jul 27, 2005 3:30 pm
by timvw
here are some snippets i've used in a quick hack
http://timvw.madoka.be/demo/person_list.php (If you use % in a value for the search, it will use LIKE instead of =)
From the list page:
Code: Select all
if (isset($_POST['select']))
{
$selections = $_POST['select'];
}
// handle a task
if (isset($_POST['task']))
{
foreach($tasks as $task)
{
if ($_POST['task'] == $task['name'])
{
$_SESSION['selections'] = $selections;
$_SESSION['previous'] = $_SERVER['PHP_SELF'];
header("Location: {$task['url']}");
exit();
}
}
}
From the delete page: (i display each entry again, and ask for confirmation.. Of course you could also loop through the selections..)
Code: Select all
if (isset($_POST['action']))
{
switch($_POST['action'])
{
case 'Cancel':
header("Location: {$_SESSION['previous']}");
exit();
case 'Delete':
$current_page = $_POST['page'];
$rows = $db->__get('rows');
$row = $rows[$_SESSION['selections'][$current_page - 1]];
$db->Delete($row);
removeitem($current_page);
break;
}
}