how to resend data from action to form page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
prakki79
Forum Newbie
Posts: 1
Joined: Sun Nov 13, 2011 12:46 am

how to resend data from action to form page

Post by prakki79 »

Hi,

I am newbie to PHP. If any one explain me with a simple example, it would be really great.

I have a form page "queue_info.php". After user hits SUBMIT, this page will redirect to an action page "queue_action.php".
from form page data are passed using "$quests=$_POST['quests'];" to this action page.
Now i need to resend this same data back to the form page "queue_info.php" from action page. How to do this? Appreciate your inputs on this.

thanks,
prakash
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: how to resend data from action to form page

Post by twinedev »

Assuming you have a form you are posting back to queue_info.php:

Code: Select all

<input type="hidden" name="quests" value="<?php echo htmlspecialchars($quests); ?>" />
But from the wording, it doesn't sound like that is what you are wanting, just the data to be available, so probably sessions:

Code: Select all

<?php
// This is at the start of the php script for both files
session_start();

// On the processing page
$_SESSION['quests'] = $_POST['quests'];

// On the original form, you can call:
echo htmlspecialchars($_SESSION['quests']);
Now if you are doing it for form validation, (ie they enter a form, and you detect error, and want the form to still have everything they already entered), a cleaner way would be to put them on the same page, use something similar to:

Code: Select all

<?php

    if (count($_POST)>0) { // The form was sumitted

        $aryErr = array(); // Contains form errors
        
        // BEGIN: Validate data....

            if (!isset($_POST['Name']) || strlen($_POST['Name'])<2) {
                $aryErr['Name'] = 'Name is requrired and needs to be at least two characters';
            }

            if (!isset($_POST['Email']) || !preg_match('/^[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i',$_POST['Email'])) {
                $aryErr['Email'] = 'Invalid Email Address';
            }
            else {
                $SQL = 'SELECT `Email` FROM `Users` WHERE `Email`="'.mysql_real_escape_string($_POST['Email']).'"';
                $rsCheck = mysql_query($SQL);
                if ($rsCheck && mysql_num_rows($rsCheck)>0) {
                    $aryErr['Email'] = 'That e-mail address is alreay in use.';
                }
                unset($rsCheck);
            }

        // END: Validate data....

        if (count($aryErr)==0) { // No errors on form

            // HERE YOU WOULD SEND OUT EMAIL AND/OR WRITE THINGS TO THE DATABASE
            
        } // END: if (No errors on form)
            
    } 
    else { // The form was not posted (ie first vist)

        // Optionally, you can load up values from a database, (ie, editing existing record)
        // And also set default values (ie. $_POST['State'] = 'Ohio'; )
        
    } // END-ELSE: if(Data posted)

    // These just makes less typing later and the code easier to read
    function echoHSC($strText) {
        echo htmlspecialchars($_POST[$strText],ENT_QUOTES);
    }
    function echoPost($strIndex) {
        if (isset($_POST[$strIndex])) {
            echoHSC($_POST[$strIndex]);
        }
    }

?><html>
<head>
    <title>Form Template</title>
</head>
<body>
    <h1>Fill out my form and win my grattitude</h1>
    <?php if (isset($aryErr)): ?>
        <?php if (count($aryErr)==0): ?>
        
            <h3>Thank you for filling out our form!</h3>

        <?php else: // There were errors ?>
            
            <div id="errors">
                <p>We found the following error(s):</p>
                <ul>
                    <?php foreach($aryErr as $strErr): ?>
                        <li><?php echoHSC($strErr); ?></li>
                    <?php endforeach; ?>
                </ul>
            </div>

        <?php endif; unset($aryErr); // Unset it for next IF block  ?>
    <?php endif; // end of was $aryErr set, (ie. form was submitted) ?>

    <?php if (!isset($aryErr)): ?>
    
        <form method="post" action="">
            <fieldset>
                <ul>
                    <li>
                        <label for="Name">Name*</label>
                        <input type="text" name="Name" id="Name" value="<?php echoPost('Name'); ?>" />
                    </li>
                    <li>
                        <label for="Email">Email*</label>
                        <input type="text" name="Email" id="Email" value="<?php echoPost('Email'); ?>" />
                    </li>
                    <li>
                        <label for="Comments">Comments</label>
                        <textarea cols="40" rows="5" name="Comments" id="Comments"><?php echoPost('Comments'); ?></textarea>
                    </li>
                </ul>
            </fieldset>
            <input type="submit" name="submit" value="Send Comments" />
        </form>

    <?php endif; // END: Display form ?>

</body>
</html>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to resend data from action to form page

Post by Celauran »

If you'll forgive a somewhat obvious question, if the form is part of queue_info, and queue_info needs the form data, why send it to another page for processing?
Post Reply