 |
|
|
| View previous topic :: View next topic |
| Author |
Message |
Pain
Joined: 13 Feb 2008 Posts: 19
|
Posted: Sun Apr 06, 2008 12:55 pm Post subject: form disappears |
|
|
| Is there a way to make the form (quickform) not disappear after submiting the values from it ? |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1011
|
Posted: Sun Apr 06, 2008 1:34 pm Post subject: Re: form disappears |
|
|
| Pain wrote: | | Is there a way to make the form (quickform) not disappear after submiting the values from it ? |
Yes.
If you want a more reasonable answer, you should explain the problem in more detail, maybe with some code snippets. |
|
| Back to top |
|
 |
Pain
Joined: 13 Feb 2008 Posts: 19
|
Posted: Sun Apr 06, 2008 1:59 pm Post subject: |
|
|
ok, when I make a form with html + php I fill it with data, push the submit button, the values are inserted into the DB, and the fileds in the form ale being cleaned. In the quickform afeter pushing the submit button the values are being inserted but the whole form disappears, so when I want to add more data I have to use the go back button in the web browser.
The code looks more or less like this
| Code: | <?php
// mysql connection
require 'inc/mysql.php';
// Load the main class
require_once 'HTML/QuickForm.php';
// Instantiate the HTML_QuickForm object
$form = new HTML_QuickForm('form1','POST');
// Add form elements
$form->addElement('text', 'name1', 'Write name1:', array('size' => 25, 'maxlength' => 25));
$form->addElement('text', 'name2', 'Write name2:', array('size' => 25, 'maxlength' => 70));
$form->addElement('submit', null, 'AddtoDatabase');
// Def filters and validate
$form->applyFilter('name1', 'trim');
$form->addRule('name1', 'You muste write name1', 'required', null, 'client');
// Try to validate a form
if ($form->validate())
{
//code for inserting the values from the form into the DB
}
// Output the form
$form->display();
?> |
|
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1011
|
Posted: Sun Apr 06, 2008 2:25 pm Post subject: |
|
|
Your code should display the form in all cases. If it doesn't, you're doing something in the validation block that prevents this (e.g. exit() or die() call).
BTW, I'd add a $form->isSubmitted() call to the $form->validate() call to prevent the validation on the initial call of the page:
| Code: |
if ($form->isSubmitted() && $form->validate()) {
|
|
|
| Back to top |
|
 |
Pain
Joined: 13 Feb 2008 Posts: 19
|
Posted: Sun Apr 06, 2008 4:08 pm Post subject: |
|
|
ok, found it, big thx
edit: is there a way to clear the form automatically after submiting the values ? |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1011
|
Posted: Sun Apr 06, 2008 4:53 pm Post subject: |
|
|
| Pain wrote: | | ok, found it, big thx |
What was the problem?
| Pain wrote: | | edit: is there a way to clear the form automatically after submiting the values ? |
A "hackish" way exists, yes. The clean solution is to redirect the user to your form page:
| Code: |
header('Location: http://www.example.com/path/to/your/script.php');
exit;
|
|
|
| Back to top |
|
 |
Pain
Joined: 13 Feb 2008 Posts: 19
|
Posted: Sun Apr 06, 2008 6:24 pm Post subject: |
|
|
| mark wrote: |
What was the problem?
|
exit()
dont remember why it was there
edit:
| mark wrote: |
A "hackish" way exists, yes. The clean solution is to redirect the user to your form page:
| Code: |
header('Location: http://www.example.com/path/to/your/script.php');
exit;
|
|
The script is in te "body" of a html file, so the header should be befor any html, but there must be some if, or it will redirect itself to infinity, so what should be in the "if () "? |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1011
|
Posted: Sun Apr 06, 2008 7:05 pm Post subject: |
|
|
| Pain wrote: | | The script is in te "body" of a html file, so the header should be befor any html, but there must be some if, or it will redirect itself to infinity, so what should be in the "if () "? |
It depends on what you're doing in your script. The redirect makes sense in the same case in that you're showing the form again currently (i.e. after validation and writing the data into the database). |
|
| Back to top |
|
 |
Pain
Joined: 13 Feb 2008 Posts: 19
|
Posted: Sun Apr 06, 2008 10:14 pm Post subject: |
|
|
| Code: | <?php
// mysql connection
require 'inc/mysql.php';
// Load the main class
require_once 'HTML/QuickForm.php';
// Instantiate the HTML_QuickForm object
$form = new HTML_QuickForm('form1','POST');
// Add form elements
$form->addElement('text', 'name1', 'Write name1:', array('size' => 25, 'maxlength' => 25));
$form->addElement('text', 'name2', 'Write name2:', array('size' => 25, 'maxlength' => 70));
$form->addElement('submit', null, 'AddtoDatabase');
// Def filters and validate
$form->applyFilter('name1', 'trim');
$form->addRule('name1', 'You muste write name1', 'required', null, 'client');
// Try to validate a form
if ($form->isSubmitted() && $form->validate())
{
$name1= $_POST['name1'];
$name2=$_POST['name2'];
$sql="INSERT INTO test(name1,name2) VALUES ('$name1','$name2')";
$send=mysql_query($sql);
print("<script language='Javascript'>");
print("alert ('The value has been add successful')");
print("</script>");
header('Location: http://www.example.com/path/to/your/script.php');
exit;
}
// Output the form
$form->display();
?> |
when I add the header after the JS alert the script makes the insert into the DB but the alert does not show up |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1011
|
Posted: Mon Apr 07, 2008 5:51 pm Post subject: |
|
|
| Pain wrote: | | when I add the header after the JS alert the script makes the insert into the DB but the alert does not show up |
That's not really a PEAR problem anymore, and I also wouldn't use an annoying JS alert window for the success message. I'd just do a simple redirect (via header()) if the insert query was successful. Otherwise (i.e. query failed or form not valid), I'd show an error message on the same page (in or next to the form). But that's your choice, of course.
Another suggestion: Please familarize yourself with the term "SQL injection". Using user input directly in a query without any filtering or at least some quoting or escaping is dangerous. |
|
| Back to top |
|
 |
Pain
Joined: 13 Feb 2008 Posts: 19
|
Posted: Wed Apr 09, 2008 9:04 am Post subject: |
|
|
| mark wrote: |
Another suggestion: Please familarize yourself with the term "SQL injection". Using user input directly in a query without any filtering or at least some quoting or escaping is dangerous. |
not necessarily, only the admin will have access to the form after login in |
|
| Back to top |
|
 |
|
|
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
|
|