user2398101
user2398101

Reputation: 339

white spaces in $_POST

I have a HTML dropdown list populated by PHP as following:

<form name="selectOccupation" method="post" action="specific_occupation.php">
                        <div align="centre">
                        <select name="occupation_dropdown" id="occupation_dropdown">
                        <?php
                        include "connection.php";

                        $sql = mysql_query("SELECT DISTINCT Occupation from patient_info");
                        while ($row = mysql_fetch_array($sql))  {
                echo "<option value=".$row['Occupation'].">".$row['Occupation']."</option>";
                        }
                        ?>
                        </select>
                        </div>
                        <br>
                        <br>
                                <input type="submit" name="submit" value="Proceed"/>
                    </form>

In the specific_occupation.php file, I have this code:

<?php

    include "connection.php";

    $selectedoccupation = $_POST['occupation_dropdown'];
    echo $selectedoccupation;   

    $myquery = "SELECT patient_info.Name, test_info.DateOfTest FROM                 `patient_info`,`test_info` WHERE patient_info.PatientID = test_info.PatientID AND               patient_info.Occupation = '$selectedoccupation' ";

    $query = mysql_query($myquery);

        if ( ! $query ) {
            echo mysql_error();
            die;
        }

        $data = array();

        for ($x = 0; $x < mysql_num_rows($query); $x++) {
            $data[] = mysql_fetch_assoc($query);
        }

     $tempdata = json_encode($data);
?>

This works fine if an occupation without any white space between them like "carpenter" is selected from the dropdown menu but doesn't work for "sales person". How can the $_POST be used for occupations with white spaces??

Thanks in advance !!

Upvotes: 0

Views: 1587

Answers (2)

Dumb_programmer
Dumb_programmer

Reputation: 1

PHP post replace some characters such as - with whitespaces. You can try urlencode($var) in your PHP script.

Upvotes: -2

Marc B
Marc B

Reputation: 360702

You're building your HTML wrong. You've got it as:

 <option value=Sales Person>
               ^^^^^---what will get sent as the value
                     ^^^^^^---some wonky unknown/non-standard attribute

This is incorrect. HTML now requires that ALL attributes be quoted:

 <option value="Sales Person">

and even when the quotes weren't required, you still had to have the quotes to handle "spaced" data like this.

Upvotes: 4

Related Questions