PEAR Forum :: PHP Extension and Application Repository

PEAR Forum Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Quickform select from database
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    PEAR Forum Forum Index -> General PEAR questions
View previous topic :: View next topic  
Author Message
Pain



Joined: 13 Feb 2008
Posts: 19

PostPosted: Wed Mar 05, 2008 6:52 pm    Post subject: Quickform select from database Reply with quote

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
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1235

PostPosted: Thu Mar 06, 2008 4:09 pm    Post subject: Reply with quote

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
View user's profile Send private message
Pain



Joined: 13 Feb 2008
Posts: 19

PostPosted: Thu Mar 06, 2008 4:46 pm    Post subject: Reply with quote



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
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1235

PostPosted: Thu Mar 06, 2008 4:58 pm    Post subject: Reply with quote

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
View user's profile Send private message
Pain



Joined: 13 Feb 2008
Posts: 19

PostPosted: Thu Mar 06, 2008 5:17 pm    Post subject: Reply with quote

There is no js, only php + html tags for the form
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1235

PostPosted: Thu Mar 06, 2008 5:24 pm    Post subject: Reply with quote

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
View user's profile Send private message
Pain



Joined: 13 Feb 2008
Posts: 19

PostPosted: Thu Mar 06, 2008 10:00 pm    Post subject: Reply with quote

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
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1235

PostPosted: Thu Mar 06, 2008 11:10 pm    Post subject: Reply with quote

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
View user's profile Send private message
Pain



Joined: 13 Feb 2008
Posts: 19

PostPosted: Fri Mar 07, 2008 5:08 pm    Post subject: Reply with quote

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
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1235

PostPosted: Fri Mar 07, 2008 6:23 pm    Post subject: Reply with quote

Pain wrote:
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 ?


Yes: http://pear.php.net/manual/en/package.html.html-quickform.html-quickform-select.loadquery.php
Back to top
View user's profile Send private message
Pain



Joined: 13 Feb 2008
Posts: 19

PostPosted: Fri Mar 07, 2008 7:58 pm    Post subject: Reply with quote

Code:
$s =& $form->addElement('select', 'name', 'sometext:');
$dsn = 'mysql://login:[email protected]/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
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1235

PostPosted: Fri Mar 07, 2008 8:17 pm    Post subject: Reply with quote

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
View user's profile Send private message
Pain



Joined: 13 Feb 2008
Posts: 19

PostPosted: Fri Mar 07, 2008 10:00 pm    Post subject: Reply with quote

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
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1235

PostPosted: Fri Mar 07, 2008 10:04 pm    Post subject: Reply with quote

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
View user's profile Send private message
Pain



Joined: 13 Feb 2008
Posts: 19

PostPosted: Sat Mar 08, 2008 7:54 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    PEAR Forum Forum Index -> General PEAR questions All times are GMT + 2 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



PEAR Forum topic RSS feed 
Powered by phpBB © 2001, 2005 phpBB Group

Provided by Ministry of Web developement