tags:

views:

332

answers:

3

Just wondering If there is a quick code/ short cut to get all textbox, combobox and radio button values in the current html page using php. Instead of $_Get and give names for each.

I have 4 pages in my application, where a user can go back and forth, I want to retain all values the user inputted in my page, some of the controls are static and rest are dynamically created. I just want a generic way of retaining all input boxes with values, instead of specifying names for each

+1  A: 

You can enumerate them all using a foreach loop as such:

foreach($_GET as $name => $value) {
    echo "<b>", $name, ":</b> ", $value;
}

I don't see the usefulness of doing that. Without field names, there is no way of distinguishing between multiple input areas. Maybe provide an example of exactly what you are trying to achieve?

Andrew Moore
I have 4 pages in my application, where a user can go back and forth, I want to retain all values the user inputted in my page, some of the controls are static and rest are dynamically created. I just want a generic way of retaining all input boxes with values, instead of specifying names for each.
Ramji
You would have to use JavaScript for that - assuming you are talking about something like this tabbed interface right here: http://dnn.mandeeps.com/livetabs/demo/Custom%20Javascript%20Functions.aspx.And as I said before, if you want to use PHP as your backend, you need those field names, even if you only submit the form on the last page...
Franz
A: 

If you're using GET to transfer form data, then you just need to make sure you pass all those parameters in the URL string to the next page. You could use foreach to construct the query string as such:

$returnStr = "";
foreach($_GET as $name => $value) {
    if ($name = ((array_keys($_GET)[0]))
        $returnStr .= sprintf("?%s=%s", $name, $value);
    else
        $returnStr .= sprintf("&%s=%s", $name, $value);
}

// now $returnStr is of the form "?key1=value1&key2=value2"...

Then, in your form that you are using to submit data, append $returnStr to the action attribute:

...
<form method="get" action="page2.php<?php echo $returnStr; ?>">
...

This should append any extra data fields you create to the get request. Keep in mind this would be harder to do with post... although post is a bit more secure since your values aren't just sitting in the URL bar.

I haven't actually tested this since I don't have a PHP-enable server here, but it should work..

sohum
A: 

The question is not very clear, however if you simply want to display all of the form field names and values, the easiest way is:

echo '<pre>' . print_r($_GET, 1) . '</pre>';

The print_r function is very useful when working with forms with a lot of fields.

evolve