Return to iWEBTOOL

Go Back   iWEBTOOL Talk > The Web and your Website > Programming > PHP
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
Welcome to iWEBTOOL Talk, where you talk about webmaster-related stuff.
 
Discuss topics which interest you.
With over thousands of threads (topics), we're sure you'll find something that'll interest you.
Ask for help whenever you need it.
We have thousands of members who are available to help you if you need it.
It's absolutely FREE!

 1  Register           2  Browse the board           3  Discuss whatever may interest you!
 


Reply
 
Thread Tools Search this Thread Display Modes
Old 04-09-2008, 08:16 PM   #1
jperezmt
Junior Member
 
jperezmt's Avatar
 
Join Date: Apr 2007
Posts: 92
jperezmt is on a distinguished road
Angry Form sticky, reset, etc.

I'm going a little nuts over this.

I'm creating a form for people to enter comments. The issue I'm having is when someone clicks the submit button and a required field isn't filled out, all the fields reset.

I looked into sticky forms, and I finally got them not to reset, now the only problem I'm having is they after everything is submitted, the fields don't reset.

I just need a happy medium. If they miss a required field, keep the data intact, if they fill in everything, reset the form. I can't find an answer, someone please help. Thanks!

PHP Code:
if (isset($_POST['submit']) || isset($_POST['submit_x'])) {

    if($code != 4) { 
        $badform = 1;
    }
    if (trim($nickname) == '') {
        $badform = 1;
    }
    if (trim($comment) == '') {
        $badform = 1;
    }

    if (!get_magic_quotes_gpc()) 
        {
        $nickname = addslashes($_POST['nickname']);
        $comment = addslashes($_POST['comment']);
        $code = addslashes($_POST['code']);
        } 
        
    if ($badform != 1) {
        $query = "this is the query"; /* Quotes are important in this lines statment */
        $result = mysql_query($query) or die("sorry something went wrong"); 
        
    }
}
    <form action="<?php echo "blog.php?id=$id" ?>" method=post>
<?php 
            
if(isset($_POST['submit']) || isset($_POST['submit_x']))  {
                if (
trim($nickname) == '') {
                echo 
"<p class=\"error\">What do people call you?</p>";
                } 
            }
        
?>
          <input class="textinput" name="nickname" type="text" maxlength="12" value="<?php if (isset($_POST['nickname'])) echo $_POST['nickname']; ?>"  />
         <?php 
            
if(isset($_POST['submit']) || isset($_POST['submit_x']))  {
                if (
trim($comment) == '') {
                echo 
"<p class=\"error\">At least say something!</p>";
                }
            }
        
?>
          <textarea class="texta" rows="5" name="comment"><?php if (isset($_POST['comment'])) echo $_POST['comment']; ?></textarea>
        <?php
                
if(isset($_POST['submit']) || isset($_POST['submit_x']))  
                {
                    if (
$code != 4) {
                    echo 
"<p class=\"error\">Not everyone can figure this out...think about it.</p>";
                    }
                }
        
?>
          <input class="textinput" name="code" type="text" maxlength="1" />
            <input type="hidden" name="blogid" value="<?php echo "$id" ?>" />
            <input src="/images/addComment.gif" type="image" name="submit" value="Add Comment" alt="Add Comment" />
</form>
__________________
Build Your Web Presence

Last edited 04-09-2008 at 08:26 PM. Reason: adding code
jperezmt is offline   Reply With Quote
 
This is an Ad Revenue Sharing forum Place your advert here
SEO Checklist
Get yourself better ranking with
our "Do-it-Yourself" SEO Checklist.
Click Here
Old 04-13-2008, 03:11 AM   #2
Aaron
Smurf
 
Aaron's Avatar
 
Join Date: Mar 2008
Posts: 20
Aaron will become famous soon enough
Default Re: Form sticky, reset, etc.

It isn't really bad if the fields don't reset. It happens on a lot of sites, and is a lot less annoying than the fields actually resetting.

Without even reading your code, I can tell you to just put an if statement around the code that fills in the fields
PHP Code:
if ($formsucess == 0)
{
  
//Fill in the fields if the form had an error

__________________
A passion for learning and a motivation to share the knowledge you obtain will make you truly understand what font your site's text should be. Seriously, help me.
Aaron is offline   Reply With Quote
Old 04-14-2008, 01:47 PM   #3
littleBird
Smurf
 
littleBird's Avatar
 
Join Date: Apr 2008
Posts: 9
littleBird is on a distinguished road
Send a message via Skype™ to littleBird
Default Re: Form sticky, reset, etc.

Alternatively, you could get into the habit of laying out all the different stages of the form processing clearly and distinctly so as to make your life a lot easier, especially for when your forms get more complex.

Another benefit of going with this altered structure is that you can use the same page for adding new records and updating existing ones.

Also, excuse me if I have totally gone over the top on the answer, my feeling is that bad form processing gives PHP a bad name, too many security problems et al.


Quote:
<?php


// Reset SESSION variables everytime
$_SESSION['id']['formvar'] = NULL;
$_SESSION['nickname']['formvar'] = NULL;
$_SESSION['comment']['formvar'] = NULL;
$_SESSION['code']['formvar'] = NULL;

$_SESSION['error'] = FALSE;
$_SESSION['id']['error'] = NULL;
$_SESSION['nickname']['error'] = NULL;
$_SESSION['comment']['error'] = NULL;
$_SESSION['code']['error'] = NULL;


// Check if valid Blog ID has been submitted - yes: UPDATE RECORD no:ADD RECORD
if (isSet($_GET['id']) || isSet($_POST['id'])) {
$_SESSION['id']['formvar'] = $_POST['id'];
$action = 'update';
$actionTitle = "Update Existing Record";
$path = $_SERVER['PHP_SELF']."?id=".$_GET['id'];

// Load data from existing record
$query = "SELECT * FROM table_name WHERE id='".$_SESSION['id']['formvar']."' LIMIT 1";
$result = mysql_query($query) or die("sorry something went wrong");
$data = mysql_fetch_array($result);
$_SESSION['nickname']['formvar'] = $data['nickname'];
$_SESSION['comment']['formvar'] = $data['comment'];
$_SESSION['code']['formvar'] = $data['code'];
}
else {
$action = 'add';
$actionTitle = "Add New Record";
$path = $_SERVER['PHP_SELF'];
}

// If form has bene submitted complete the following
if (isSet($_POST['submit'])) {

// ---------------------------------------------------------------------- IMPORT POST DATA
$_SESSION['nickname']['formvar'] = $_POST['nickname'];
$_SESSION['comment']['formvar'] = $_POST['comment'];
$_SESSION['code']['formvar'] = $_POST['code'];


// ---------------------------------------------------------------------- PERFORM DATA VALIDATION

// Check if valid Nickname has been submitted
if (trim ($_SESSION['nickname']['formvar'])== '') {
$_SESSION['nickname']['error'] = 'What do people call you?';
$_SESSION['error'] = TRUE;
}

// Check if valid Comment has been submitted
if (trim ($_SESSION['comment']['formvar'])== '') {
$_SESSION['comment']['error'] = 'At least say something!';
$_SESSION['error'] = TRUE;
}

// Check if valid Code has been submitted
if ($_SESSION['code']['formvar'] != -4) {
$_SESSION['code']['error'] = 'That code ain\'t no good';
$_SESSION['error'] = TRUE;
}

// ------------------------------------------------------------ End - DATA VALIDATION


// FORM PROCESSING
if (! $_SESSION['error']) {

$query = "SET nickname='".addslashes($_SESSION['nickname']['formvar']).", comment='".addslashes($_SESSION['comment']['formvar']);

if ($action == 'add') {
$query = "INSERT INTO table_name ".$query;
}
else {
$query = "UPDATE table_name ".$query." WHERE id='".$_SESSION['id']['formvar']."'";
}

$result = mysql_query($query) or die("sorry something went wrong");
}
}
// -------- End - FORM PROCESSING
?>

<html>
<head></head>
<body>

<form action="<?= $path; ?>" method=post>
<input type="hidden" name="id" value="<?= $_SESSION['id']['formvar']; ?>" />

<fieldset>
<legend><?= $actionTitle; ?></legend>
<p>
<label for="nickname">Nickname
<?php if (! empty($_SESSION['nickname']['error'])) { echo "<br><span class='error'>".$_SESSION['nickname']['error']."</span>"; } ?>
</label>
<input class="textinput" name="nickname" type="text" maxlength="12" value="<?= $_SESSION['nickname']['formvar']; ?>" />
</p>

<p>
<label for="comment">Comment
<?php if (! empty($_SESSION['comment']['error'])) { echo "<br><span class='error'>".$_SESSION['comment']['error']."</span>"; } ?>
</label>
<textarea class="texta" rows="5" name="comment"><?= $_SESSION['comment']['formvar']; ?></textarea>
</p>

<p>
<label for="comment">Code
<?php if (! empty($_SESSION['code']['error'])) { echo "<br><span class='error'>".$_SESSION['comment']['error']."</span>"; } ?>
</label>
<input class="textinput" name="code" type="text" maxlength="2" />
</p>

<input src="/images/addComment.gif" type="image" name="submit" value="Add Comment" alt="Add Comment" />
</fieldset>
</form>

</body>
</head>
littleBird is offline   Reply With Quote
 
This is an Ad Revenue Sharing forum Place your advert here
Webmaster Tools Webmaster Tools Click Here
Reply



Quick Reply
Message:

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Where can I purchase a sticky?? pc_s Help and Support 8 11-28-2007 11:28 PM
When do top sites rankings reset buldozerceto Help and Support 1 11-04-2007 09:02 PM
Sticky Link Territory-Bidding Directory Sticky Advertise your website 0 09-24-2007 08:08 AM
Sticky sites, traffic and Google's algo jfranktoo Google 5 07-03-2007 06:31 PM
Suggest of New Section which is sticky also. davidcheong Suggestions & Feedback 0 03-11-2007 04:57 PM


All times are GMT. The time now is 07:55 AM.


Powered by vBulletin v3.6.7 © 2008, Jelsoft Enterprises Ltd. SEO by vBSEO © 2006, Crawlability, Inc.