Welcome Guest, Not a member yet? Register   Sign In
help with form array display results

Consider this scenario:

I'm using an ajax to make the form use only one page

I got a multistep form with step3 being the result of step2, within step3 there is an "add more steps" button which duplicates step2 and step3, this can be done countless times.

to complement this I added array to my input boxes something like this:

<input type="text" name="name[]" id="name"/>

unfortunately, I'm not sure how to approach this in the php.
In the step2 going to step3, I would call the brand id and use it in the database to get the results for step3,

but now it's array, so I added the following lines
$brand = $this->input->post('brand[]');
$q = $this->MBest->getbestmatching($brand[0]);

But the $brand[0] doesn't works and triggers an error.

And come to think of it, if I call a $brand[0] index, then everytime a person add a new step, it will always use the id of the first result.

Are there any ideas on how to fix this problem? Thanks

I tried doing the following code in the controller that is responsible for passing step2 data to step3

$brandID = $this->input->post('brand[]');
    $brand[] = $brandID[$x];
$q = $this->MBest->getbestmatching(end($brand));

I get
Array ( [0] => )
in the result.

But when I look at the ajax request in firebug
I found that brand did post a value.

Where did I go wrong?

not sure I 100% understand your use case... but I do see one odd thing that stands out. Its how you are trying to retrieve the post variable. Drop the brackets. Brackets are used for setting an array, not for retrieving it (I am sure that is far to general to be true for all php, but it is in this case)

Look at this case:
&lt;input type="checkbox" name="ckbx[]" value="val1" /&gt;Value 1<br />
&lt;input type="checkbox" name="ckbx[]" value="val2" /&gt;Value 2<br />
&lt;input type="checkbox" name="ckbx[]" value="val3" /&gt;Value 3<br />
if you were to post that then you would retrieve it using
<- note the difference.

run a die(var_dump($_POST)) and check it out (whether via normal post or ajax that will work)

[EDIT] for clarification, the input helper follows the same rules as if you were retrieving it using $_POST. I just ran a test using both, lemme know if it doesn't work for you, but I'd need more info to duplicate your same setup.

Thanks it works!

Unfortunately, I found another problem I realize that my form has a back and next button.

That means that when I clone the step2 and step3, going back to an earlier step2 would get the latest step3 brand id instead of the current one.

Then using
would not be applicable

Is there a way to make the code understand which index it should be fetching?

Thanks again

If you want a real answer I'd need to see a bit more example code so that I could replicate it and do some tests... but what comes to mind is that you could probably fairly easily add a variable to be passed to your next and prev functions... and that variable could be the index you are currently at.

For example, we have 1 > 2.1 > 3 - in step three the prev button would be prev(0), and you could then know to get the 0 index of the array.

You then click on the add new step button and now your form looks like this: 1 > 2.1 > 2.2 > 3 - when you create step 2.2 you would set its prev button to prev(0), and the prev button for 3 to prev(1), that way you are passing the index to look for.

Again, that's about as much help as I can be from a logical standpoint without seeing and hacking at some test code.

It's hideously long, is it okay?

[quote author="Corbee" date="1283433283"]It's hideously long, is it okay?[/quote]

The code? in that case you could use a zip/rar/tar or something with a service like dropbox. or you could throw up the pertinent parts on something like pastebin.

Also I just took the time to look below here, but it looks like you can attach files to these posts. Maybe that'd do the trick... attach a zip of the files, and then in your post highlight the portions of interest.

I just found a way to make it work without using array index.

But thanks, I really appreciate your willingness to help people out.

Theme © iAndrew 2016 - Forum software by © MyBB