Jaime Cross
Jaime Cross

Reputation: 523

PHP: Parsing of a given form input variable

How would I go about parsing incoming form data where the name changes based on section of site like:

<input type="radio" name="Motorola section" value="Ask a question">

where 'Motorola section may be that, or Verizon section, or Blackberry section, etc.

I do not have any control over changing the existing forms unfortunately, so must find a way to work with what is there.

Basically, I need to be able to grab both the name="" data as well as its coresponding value="" data to be able to populate the email that gets sent properly.

Upvotes: 1

Views: 1743

Answers (3)

user499054
user499054

Reputation:

Well, first off, you shouldn't have spaces in the name field (even though it should work with them).

Assuming it's a form, you can get the value through the $_POST (for the POST method) and $_GET (for the GET method) variables.

<?php
if ( isset( $_POST['Motorola section'] ) ) // checks if it's set
{
   $motoSec = $_POST['Motorola section'];  // grab the variable
   echo $motoSec; // display it
}
?>

You can also check the variables using print_r( $_GET ) or print_r( $_POST ).

Upvotes: 0

mario
mario

Reputation: 145482

Well, you don't receive a HTML form, but just field names and values in $_POST. So you have to look what to make out of that.

Get the known and fixed fields from $_POST and unset() those you've got [to simplify]. Then iterate over the rest. If " section" is the only constant, then watch out for that:

foreach ($_POST as $key=>$value) {
    if (stristr($key, "section")) {
        $section = $value;
        $section_name = $key;
    }
}

If there are multiple sections (you didn't say), then build an section=>value array instead.

Upvotes: 2

MSD
MSD

Reputation: 349

<form action="formpage.php" method="post">
<input type="radio" name="Motorola_section" value="Ask a question">


</form>
$motorola = $_POST['Motorola_section'];
if ($motorola =='Ask a question')
{
form submit code if motorola is selected
}

Upvotes: 0

Related Questions