user1043816
user1043816

Reputation: 139

php get select option and text input

Hello I'm trying to get a selected value and do stuff when it's selected. Also trying to print that textbox (print is just temp)

<form name="recAdd" method="GET" action="add.php">
    <select name="rec">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select><br>
    <input type="text" name="name" />
    <input type="submit" value="Submit" />
</form>

PHP (add.php):

switch($_POST['rec']){
case '1':
    $rec1 = "rec1.txt";
    $fh = fopen($rec1, 'a') or die("can't open file");
    $romString = $_GET['name'];
    fwrite($fh, $romString);
    fclose($fh);
    print()
break;
case '2':
    // do Something
break;
case '3':
    // do Something
break;
default:
    print("Not working ;(");
}

Without doing all that looking at textbox and dropdown I used this and it worked (just to see if writing to file was fine, etc.)

$recAll = "recAll.txt";
$fh = fopen($recAll, 'a') or die("can't open file");
$stringData = "New Stuff 1\n";
fwrite($fh, $stringData);
$stringData = "New Stuff 2\n";
fwrite($fh, $stringData);
fclose($fh);

Upvotes: 0

Views: 3535

Answers (3)

Theraot
Theraot

Reputation: 40180

Because the public requested it! This is my comment extended to be a full answer:

You are using both $_GET and $_POST, this is not common, are you doing this on purpose? My guess is: no.

If you see in your HTML you have:

<form name="recAdd" method="GET" action="add.php">

This means the request from the browser will be a GET. You could either change it to POST:

<form name="recAdd" method="POST" action="add.php">

PHP (add.php):

switch($_POST['rec']){
case '1':
    $rec1 = "rec1.txt";
    $fh = fopen($rec1, 'a') or die("can't open file");
    $romString = $_POST['name']; //<---
    fwrite($fh, $romString);
    fclose($fh);
    print 'something'; //<<--- fixed syntax, btw: I would use echo
break;
case '2':
    // do Something
break;
case '3':
    // do Something
break;
default:
    print("Not working ;(");
}

Note: I have tested with this variation and it is working.

Or change it all to GET (I recommend to stay with POST)

switch($_GET['rec']){ //<-----
case '1':
    $rec1 = "rec1.txt";
    $fh = fopen($rec1, 'a') or die("can't open file");
    $romString = $_GET['name'];
    fwrite($fh, $romString);
    fclose($fh);
    print 'something'; //<<--- fixed syntax, btw: I would use echo
break;
case '2':
    // do Something
break;
case '3':
    // do Something
break;
default:
    print("Not working ;(");
}

Make sure the web server (ie Apache) has rights to write in the destination folder. This may prevent the operation to success.

You could also turn on error messages from PHP, that may give insight on what is going on. You can do it for the current PHP script with this:

error_reporting(E_ALL | E_STRICT);

Note: error_reporting at php.net


If you are not sure what request method was used you can try $_SERVER['REQUEST_METHOD'] like so:

if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
    //Ok we got a POST, probably from a FORM, read from $_POST.
    var_dump($_PSOT); //Use this to see what info we got!
}
else
{
    //You could assume you got a GET
    var_dump($_GET); //Use this to see what info we got!
}

Note: $_SERVER at php.net

You may also be interested in PHP Session management and Post-Redirect-Get. With that you could do this:

if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
    //Ok we got a POST, probably from a FORM, read from $_POST.
    var_dump($_PSOT); //Use this to see what info we got!
    //Do stuff...
    //Write results to session
    session_start();
    $_SESSION['stuff'] = $something;
    //redirect:
    header('Location: add.php', true, 303);
    //The redirection will cause the browser to request with GET
    //The results of the operation are in the session variable
    exit();
}
else
{
    //You could assume you got a GET
    var_dump($_GET); //Use this to see what info we got!
    //Get stuff from session
    session_start();
    if (array_key_exists('stuff', $_SESSION))
    {
       $something = $_SESSION['stuff'];
       //we got stuff
       //later use present the results of the operation to the user.
    }
    //clear stuff from session:
    unset($_SESSION['stuff']);
}

Note: $_SESSION at php.net

Upvotes: 2

zsaat14
zsaat14

Reputation: 1118

Your form is using the get method to send the data to add.php. However, you are using $_POST to retrieve the rec value. There are two ways you can fix this:

Method 1:

Change <form name="recAdd" method="GET" action="add.php"> to <form name="recAdd" method="POST" action="add.php">. Also change $romString = $_GET['name']; to $romString = $_POST['name'];.

Method 2:

Change switch($_POST['rec']){ to switch($_GET['rec']){. This will work as long as your form's method is get.

Method 3:

Change switch($_POST['rec']){ to switch($_REQUEST['rec']){. This will work no matter what your form's method is. $_REQUEST includes the values from both $_POST and $_GET.

I have listed these solutions in order from most desirable to least. Mixing $_GET and $_POST is generally not an optimal solution.

Upvotes: 0

Mike
Mike

Reputation: 45

Change the following...

<form name="recAdd" method="POST" action="add.php">

$romString = $_POST['name'];

Upvotes: 0

Related Questions