Welcome Guest, Not a member yet? Register   Sign In
Assistance needed on form!!
#1

[eluser]Andy Harris[/eluser]
Hi guys. We had a requirement for a web form from a client which is to work *exactly* like this one.

I wanted to try this in Codeigniter because I like the form helpers etc and starting from scratch would be a drag. Plus it's a chance to use CI on something that will go live.

Anyways, what I have almost works but I think I've gone about it a bit clumsily. For starters I've gone without a model (possibly will enhance it later and add one in), but for the meantime all the meat is within a controller.

The requirement is a simple form submit and save to DB. That's all working just fine - the problem comes with the requirement for a 'tell another friend' button.

I've tried to do this by using the CI session library. When the user first comes to the page, it populates the 'refer count' variable to 1 (which is passed to the view to create the "1st" in "1st friend or family member" - when they submit by the 'tell another friend' button, it does all the DB stuff, then retrieves their user id and adds that to session along with the refer count + 2. This means on the second pass, the page will say '2nd friend etc' and once they submit it'll find an existing used id and the friends table in the database will contain the same source ID.

The problem I'm having at the moment is that when the form reloads, all the data is still there in the form. What I want to happen is for the source data (top piece) to stay intact, and the bottom section (the friend/family member) to clear. I'm struggling to do this, and it seems like it should be pretty simple.

Does anyone have any assitance, or any tips as to whether what I'm doing could be improved?

Thanks for your time, hope I made sense!
#2

[eluser]Andy Harris[/eluser]
A little more info, I'm using the CI validation library too. So each of my form fields looks a little like this:

Code:
<p><label for="source_phone" &lt;? if($this->validation->source_phone_error!="")echo 'class="error"';?&gt;>Home Phone*</label>
&lt;input type="text" id="source_phone" name="source_phone" value="&lt;?=$this-&gt;validation->source_phone;?&gt;" &lt;? if($this->validation->source_phone_error!="")echo 'class="error"';?&gt; /></p>

I guess I could pass back something to the view to tell it the form was submitted, and not to include that value if that's the case, but it's not very elegant.
#3

[eluser]xwero[/eluser]
What does the tell a friend button exactly do? If you only want to remove the input data you can set the validation->fieldname value to an empty string in your controller.

A tip for the het html snippet. You could rewrite it like
Code:
<p&lt;? if($this->validation->source_phone_error!="")echo 'class="error"';?&gt;><label for="source_phone" >Home Phone*</label>
&lt;input type="text" id="source_phone" name="source_phone" value="&lt;?=$this-&gt;validation->source_phone;?&gt;" /></p>
Less php code in your view and less classes in your css.
#4

[eluser]Andy Harris[/eluser]
I do indeed only want to remove the input data. The tell another friend does the same as the submit, except it comes back to the form and not a 'thanks' page, the idea being that they can fill in a 2nd, 3rd, 4th friend (etc etc). So I wanted it to remember the user but not the friend.

Cheers for the HTML snippet. That's something I usually spot right at the end, but not at the time I do it. In this case, the error was in the input to begin with, then I added it to the paragraph too without even thinking about it!

I shall give this a bash.
#5

[eluser]xwero[/eluser]
I think you don't need a session to get the number of the friend. You can do something like
Code:
if(!$this->validation->run)
{
   // error
}
else
{
   // clear friend input values
   foreach($_POST as $input)
   {
      if(strncasecmp('friend_',$input,7) == 0)
      {
         $_POST[$input] = '';
      }
   }
   // add friend to db and return next number
   $data['friendnr'] = $this->somemodel->addfriend($_POST);
}
$this->load->view('form',$data);
#6

[eluser]Andy Harris[/eluser]
Hmmm. That looks like it might be a much cleaner way than the way I'm trying to do it - perhaps I'm overcoding it...

Is there such a term as 'overcoding'? Cos if not, there is now!!




Theme © iAndrew 2016 - Forum software by © MyBB