Welcome Guest, Not a member yet? Register   Sign In
How do I pass Array from one view to another using POST?
#1

[eluser]faradayent[/eluser]
I'm implementing a site search via Zend (works great). I have 3 views:
A. Has a text form where the user enters search terms like: +fruit +apple
B. Is a results page where the user chooses via buttons which page to go to.
C. Is the destination page containing the searched for words.

I intend to colorize the searched words in page C so I need to pass the info clear through. I'm trying to use POST in both transfers. A to B works fine. Both +fruit and +apple can be rendered on B. But when I go from C to D all I get is +fruit, +apple is dropped (as seen with $this->output->enable_profiler(TRUE)Wink.

So the info come in B, but I'm not sending it out properly. Code B is somewhat as follows:

Code:
<?php echo $_POST['search_query'];?> // This renders OK

<?php $temp = "<FORM METHOD=POST ACTION=\"http://www.xyz9876.com/";
echo $temp . $result->path . "\">"; ?>
<INPUT type="submit" value="Go!">

<?php echo "<INPUT type=\"hidden\" name=\"search_query\" value=" . $this->input->post('search_query'); ?> // This doesn't make it
</FORM>

I've tried several things to no avail. I'm a C++ guy and always get stumped when I get back into PHP/HTML so any help would be appreciated. BTW I'd rather use HREF than a button but I don't think HTML supports POST through HREF (am I wrong?).
#2

[eluser]takasia[/eluser]
POST through HREF >>> just run the form with javascript

href="#" onclick="document.YourFormName.submit()"


And about the values - are you sure the html code is fine?

For the hidden input part try:

echo '<input type="hidden" name="search_query" value="'.$this->input->post('search_query').'">;

Be careful with ' and " -> " inside a ' does'n have to be escaped and it's easier to not get lost with " HTML needs.
#3

[eluser]JHackamack[/eluser]
If you're only getting one value passed to step C. Then try changing the input name to search_query[] if your search query post is an array. And wrap the whole thing around a foreach statement to go through each element individually.
#4

[eluser]faradayent[/eluser]
Thanks for both replies. I tried cleaning up the HTML but that didn't help. Here's the line of code with the []'s in it:

Code:
echo "<INPUT type=\"hidden\" name=\"search_query\" value=" . $this->input->post('search_query[]') . ">";

PHP rendered it as: <INPUT type="hidden" name="search_query" value=> which didn't work.

I guess what I don't understand is why CI / PHP passes the value so naturally from A to B, and yet I can't seem to "format" it correctly to get it out of B even though it's right there. In A I just use a regular form with a box that the user fills in and the system does it automatically. Even passes quotes through correctly. How does it do it?

How is the POST variable structured? String, or array? I guess I'll have to start looking for the source tomorrow. Or maybe I'll just try serialization...
#5

[eluser]takasia[/eluser]
String when text input, array when chekboxes etc.
String when the name is like "name", array when "name[]".
If you want to pass an array, the name of the first form must have the "[]" too.

If $_POST['search_query'] shows fine (you said it renders OK -> what does it show?) and $this->input->post('search_query') not (try to echo both), it means that you have something wrong with input class.
It should load automatically, but maybe doesn't or is broken somehow?

What about passing the words as a variable between the functions instead of the views?

The colorizing part could sit in a model, so you would just call it someFunc($word) where $word would be the search terms (or an array, whatever you need).

I don't know if it would do for you, but you could do something like this:

UI:
form (A)-> second form (I guess? the page with buttons, B) -> colorized serach results ©

APP:

function formA() -> do the validation if you need, just show the serach form with view A, the form action like this: 'your.php/controller/pageB/'

function pageB($button= null) ->
first get the $word =$this->input->post('search_query'); and pass it to the colorizing model $colorized_word = $this->your_model->colorize($word);
second, when no button was pushed and $button == "" show viewB (don't need to put the $word here, it will stay in the controller function fine), if the $button was selected pass the $colorized_word to viewC as show it.
#6

[eluser]faradayent[/eluser]
It's kind of funny how the things you think will be hard (like getting the Zend search function going) turn out to be easy, and something easy like passing a variable turn out to be a nightmare. :-S

I think I'll stay away from any complicated use of POST in the future. Didn't even work with serialization.

I finally decided to serialize the search string then put it in a CI session variable. Seems to work.

To put it in:
Code:
$data_search = array(
        'search_string' => serialize($this->input->post('search_query') )
    );
$this->session->set_userdata($data_search);

To retrieve it:
Code:
<?php
echo unserialize( $this->session->userdata('search_string'));
?>

Codeigniter is Great, but PHP is a bit too magical for me :lol:




Theme © iAndrew 2016 - Forum software by © MyBB