| View previous topic :: View next topic |
| Author |
Message |
Pain
Joined: 13 Feb 2008 Posts: 19
|
Posted: Wed Mar 05, 2008 6:52 pm Post subject: Quickform select from database |
|
|
I've got a problem with the select made by the quickform. I get the option elements from a mysql database.
With mysql_query i get the data and push it into a array variable ($array), then attach it to the select like this:
$form->addElement('select', 'name', 'description:',$array);
I choose an option from the select and after pushing the button it is been delete from the databse but it is still is shown in the select until I refresh the page.
When I make a select myself I just put the form html tags on the end off the script and it is automatically refreshing the content, but I don't know how to do it with quickform, is it possible to make it work this way ? |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1048
|
Posted: Thu Mar 06, 2008 4:09 pm Post subject: |
|
|
| I don't understand your problem, or better said, I don't understand what your goal is. You're writing a button. What is this? What does he do? Do you have some JavaScript magic? |
|
| Back to top |
|
 |
Pain
Joined: 13 Feb 2008 Posts: 19
|
Posted: Thu Mar 06, 2008 4:46 pm Post subject: |
|
|
when you make a script without quickform, you just make someting like that:
| Code: |
<?php
//deleteing the element that has been choosen
?>
<form>
//Select list + submit
</form>
|
And it disappears automatically without refreshing the site
So my problem is how to do it with quickform |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1048
|
Posted: Thu Mar 06, 2008 4:58 pm Post subject: |
|
|
| Pain wrote: | | And it disappears automatically without refreshing the site |
Again, how do you delete the entry? Do you use JavaScript and/or AJAX? Or is your button in your normal form a normal submit button?
Or with other words: What do you expect to happen? A JavaScript call? A submission of the form? |
|
| Back to top |
|
 |
Pain
Joined: 13 Feb 2008 Posts: 19
|
Posted: Thu Mar 06, 2008 5:17 pm Post subject: |
|
|
| There is no js, only php + html tags for the form |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1048
|
Posted: Thu Mar 06, 2008 5:24 pm Post subject: |
|
|
| Pain wrote: | | There is no js, only php + html tags for the form |
Then I still don't know what the problem is. If you insert a normal submit button with HTML_QuickForm, and you'll delete the record from the database after the form is submitted, the list should be updated.
Hmm, maybe an idea: Do you add the 'select' element (and the items of it) before you remove the record from the database? If yes, I'd add the element first, delete then (if the form was submitted), and load the database contents then to the element. |
|
| Back to top |
|
 |
Pain
Joined: 13 Feb 2008 Posts: 19
|
Posted: Thu Mar 06, 2008 10:00 pm Post subject: |
|
|
It looks more or less like this (and the list is not being refreshed after submiting the form):
| Quote: |
<?php
//connection to db
require 'inc/mysql.php';
//geting data from db for the select
$testvar=mysql_query("select * from test");
$arrayvar=array('');
while($row = mysql_fetch_array($testvar,MYSQL_ASSOC))
{
array_push($arrayvar,$row['name']);
}
// Load the main class
require_once 'HTML/QuickForm.php';
// Instantiate the HTML_QuickForm object
$form = new HTML_QuickForm('form1','POST');
$form->addElement('header', null, 'test');
$form->addElement('select', 'data', 'sometext:',$arrayvar);
$form->addElement('submit', 'deletefromdb' , 'Del');
//when submit the option is being delete form db
if (isset($_POST['deletefromdb']))
{
$id=$_POST['data'];
$var=$arrayvar[$id];
$delete=mysql_query("delete from test where name='$var'");
}
// Output the form
$form->display();
?>
|
|
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1048
|
Posted: Thu Mar 06, 2008 11:10 pm Post subject: |
|
|
| Well, my idea was right then. That's not a QuickForm problem, your code logic is just wrong. => Delete first, then fetch the records from the database. |
|
| Back to top |
|
 |
Pain
Joined: 13 Feb 2008 Posts: 19
|
Posted: Fri Mar 07, 2008 5:08 pm Post subject: |
|
|
thx it works
One more thing: Is there any other way(then the one I use) to query the data from the database and attach it to the select ? |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1048
|
|
| Back to top |
|
 |
Pain
Joined: 13 Feb 2008 Posts: 19
|
Posted: Fri Mar 07, 2008 7:58 pm Post subject: |
|
|
| Code: | $s =& $form->addElement('select', 'name', 'sometext:');
$dsn = 'mysql://login:pass@host/db';
$s->loadQuery($dsn,'SELECT name FROM test');
$form->addElement('submit','send', 'del'); |
I did someting like this, it did insert the options from the DB into the select, but I cant get the submited value from the select by using $_POST, is there some function necessary ? |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1048
|
Posted: Fri Mar 07, 2008 8:17 pm Post subject: |
|
|
| Pain wrote: | | I did someting like this, it did insert the options from the DB into the select, but I cant get the submited value from the select by using $_POST, is there some function necessary ? |
You've just used another name for the select element than in your previous post ('name' vs. 'data').
I'd recommend to use QuickForm's method for this purpose though:
| Code: |
if ($form->isSubmitted() && $form->validate()) {
$data = $form->exportValues();
// delete the record here, the id is in $data['name'] or $data['data'] or ...
}
|
|
|
| Back to top |
|
 |
Pain
Joined: 13 Feb 2008 Posts: 19
|
Posted: Fri Mar 07, 2008 10:00 pm Post subject: |
|
|
| Code: | if ($form->isSubmitted() && $form->validate()) {
$data = $form->exportValues();
print_r($data);
} |
It did'nt work, so I used print_r to se whats inside and it the result:
| Quote: | | Array ( [name] => [send] => del ) |
So the array does not containt the id of the submited value |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1048
|
Posted: Fri Mar 07, 2008 10:04 pm Post subject: |
|
|
| Pain wrote: | | Code: | if ($form->isSubmitted() && $form->validate()) {
$data = $form->exportValues();
print_r($data);
} |
It did'nt work, so I used print_r to se whats inside and it the result:
| Quote: | | Array ( [name] => [send] => del ) |
So the array does not containt the id of the submited value |
It would contain the value if you would assign a value to your select box items. As you only fetch one column ('name') from the database, it isn't a surprise that there is no id ... |
|
| Back to top |
|
 |
Pain
Joined: 13 Feb 2008 Posts: 19
|
Posted: Sat Mar 08, 2008 7:54 pm Post subject: |
|
|
| Ok, I get it now, but when I keep the order => first delete, second fetch then I can't get the id from $data['name'] since the array is filled later |
|
| Back to top |
|
 |
|