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 
form disappears

 
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: Sun Apr 06, 2008 12:55 pm    Post subject: form disappears Reply with quote

Is there a way to make the form (quickform) not disappear after submiting the values from it ?
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1235

PostPosted: Sun Apr 06, 2008 1:34 pm    Post subject: Re: form disappears Reply with quote

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



Joined: 13 Feb 2008
Posts: 19

PostPosted: Sun Apr 06, 2008 1:59 pm    Post subject: Reply with quote

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



Joined: 07 Jan 2007
Posts: 1235

PostPosted: Sun Apr 06, 2008 2:25 pm    Post subject: Reply with quote

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



Joined: 13 Feb 2008
Posts: 19

PostPosted: Sun Apr 06, 2008 4:08 pm    Post subject: Reply with quote

ok, found it, big thx


edit: is there a way to clear the form automatically after submiting the values ?
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1235

PostPosted: Sun Apr 06, 2008 4:53 pm    Post subject: Reply with quote

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



Joined: 13 Feb 2008
Posts: 19

PostPosted: Sun Apr 06, 2008 6:24 pm    Post subject: Reply with quote

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



Joined: 07 Jan 2007
Posts: 1235

PostPosted: Sun Apr 06, 2008 7:05 pm    Post subject: Reply with quote

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



Joined: 13 Feb 2008
Posts: 19

PostPosted: Sun Apr 06, 2008 10:14 pm    Post subject: Reply with quote

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



Joined: 07 Jan 2007
Posts: 1235

PostPosted: Mon Apr 07, 2008 5:51 pm    Post subject: Reply with quote

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



Joined: 13 Feb 2008
Posts: 19

PostPosted: Wed Apr 09, 2008 9:04 am    Post subject: Reply with quote

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
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
Page 1 of 1

 
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