Pass Data from Form to Controller and Back Again.

Permalink
Hello Community,

Let me first say, I'm somewhat of a nub when it comes to Concrete5/PHP/Ajax/JavaScript.

I have inherited a website that uses Concrete5 and an external form. It is now my responsibility to finish this. The external form posts to its controller page (works great). There is a lot of client side JavaScript for client side validation, but there may be visitors that dont use JavaScript for whatever reason.

When the form is submitted if there's any required fields (or wrong Captcha) I need to be able to return to the calling form and re-populate the fields that were filled in so the visitor doesnt lose entered data and generate some kind of response saying "Hey fill in all your fields!"

Another thing that perplexes me is the return call. What do you return to? Index.php doesnt have any code in it, just uses some dispatcher and then somehow inserts/themes my external form. How you do even go about getting variables from the controller with all the magic behind the scenes?

I'm thinking either hidden fields (which I tried, but I must have some scope issue because the hidden fields never have the values that the php code above have) or I thought about session variables and $GLOBALS and I dont understand how to get these to work.

I have spend gobs of time looking at lots of forum posts and tried to do what they are doing and just cant get it to work. Even a simple working example would be wonderful.

I'll try to keep the code as short as possible for readability. Any help would be most appreciated. Please see attached text files.

Thanks so much in advance!
James

2 Attachments

jyoung
 
jyoung replied on at Permalink Reply
jyoung
I figured out my problem on my own... dont need any help.

Thanks,
James
pmarques replied on at Permalink Reply
pmarques
It would be nice to post your solution, so you can help others who run into this question.
jyoung replied on at Permalink Best Answer Reply
jyoung
Sure thing, I can do that. I didnt bother posting a solution because I just assumed since no one responded, that no one saw it. Sorry I'm new to this whole forum thing. :)

The controller is the key to repopulating fields. When the external form is posted, the controller is the object that captures the value via post.

// form post method (note the action, this calls the controller)
<form name='contactus' id='contactus' method="post" action="<?php echo $this->action('submit_form')?>" onsubmit="return validate(this);">

*Note: The controller must have a public function named: "action_submit_form()" or it wont work

// Controller variable to catch the posted value
$_POST['fHomePhone'];

While in the controller if you find that the captcha is wrong you will need to "set" a variable so that the external form block can gain access to this variable. This is done so the visitor doesnt lose data.

Ex: (*Still in the controller)
if ($errors == 0)
{
// either send results to email or database
}
else
{
// set variable for re-population
$this->set('fHomePhone', $_POST['fHomePhone']);
}

The following is a field in the external form block whose value is now the variable that was set in the controller.

<input type="text" id="fHomePhone" size="25" name="fHomePhone" value="<?php echo $fHomePhone; ?>">

The variable $fHomePhone in the external form block does not have a value when it first loads. It will only get "set" in the controller if there is an error.

Hope that is not clear as mud :)

Take care,
James
stephendmalloy replied on at Permalink Reply
stephendmalloy
This is a wicked old thread - but if you use the C5 form helper you don't have to set any form fields - it does it for you :)

For example:

$form = Loader::helper('form');
echo $form->text('first_name', $first_name, array('maxlength' => 15));