AJAX + PHP self-generated fields producing blank r

2019-09-21 02:59发布

问题:

I'm back and this one might be a little tricky but we will find out! :D

Okay I am using php to self populate my select fields so I don't have to continuously update the options when new agents join. I am using ajax (javascript) for this part of the page and I have had no problems with this except with the company select field.

Live site

All my select fields work with out any problems but the Company field which will display nothing only on companies that seem to have "&" in the name. (Two companies still work with "&" in the name)

List of non-functioning Companies:
-Anthem
-Bogart
-Burnham
-Church Insurance
-Fawcett
-JRM
-Kenneth B. Brown
-Newton
-Sam F.
-Sherrill
-Wallace & Turner

PHP Company select code: (The if/else statement in the companies field would ideally be if ($row['Company'] !== NULL) { echo '<option value="'.$row['Company'].'">'.$row['Company'].'</option>'; } but for some reason it will echo in a blank space in the option dropdown.)

<label for="company">Company</label><br />
<select id="company" name="users" onChange="showUser(this.value)">
       <?php include 'login.php';

        $result = mysqli_query($con, "SELECT DISTINCT Company FROM `roster` ORDER BY Company ASC;");
                    echo '<option value="">' . 'Select a Company' .'</option>';
                    while ($row = mysqli_fetch_array($result)) {
                        if ($row['Company'] == NULL) {

                        }
                        else {
                            echo '<option value="'.$row['Company'].'">'.$row['Company'].'</option>';
                        }
                    }
        ?>
</select>

PHP for other select fields:

<label for="last">Last Name</label><br />
                <select id="last" name="Last_Name" onChange="showUser(this.value)">
                    <?php include 'login.php';

                    $result = mysqli_query($con, "SELECT DISTINCT Last_Name FROM `roster` ORDER BY Last_Name ASC;");
                    echo '<option value="">' . 'Select an Agent' .'</option>';
                    while ($row = mysqli_fetch_array($result)) {
                        echo '<option value="'.$row['Last_Name'].'">'.$row['Last_Name'].'</option>';
                    }
                    ?>
</select>

Any Ideas on this one? If you need to see the process.php page which echos in the results then just ask! Thanks a million to anyone who helps out.

How the HTML looks when populated through php:

<select id="company" name="users" onchange="showUser(this.value)">
     <option value="">Select a Company</option><option value="A.R.T. Group">A.R.T. Group</option><option value="ALPHA Benefits">ALPHA Benefits</option>                    
</select>

I only included a few since 80ish of them is one massive line of html.

回答1:

You need to urlencode the parameter that is being passed via ajax. The ampersand is telling PHP that $_GET['q'] is complete and a new $_GET variable is starting.

Notice the %26 for the ampersand returns the desired result.

http://healthbenefitsohio.com/process.php?q=Anthem%20Blue%20Cross%20%26%20Blue%20Shield

Your company options should look like this:

echo '<option value="'.urlencode($row['Company']).'">'.$row['Company'].'</option>';

For good measure, I would also encode the display

echo '<option value="'.urlencode($row['Company']).'">'.htmlspecialchars($row['Company'],ENT_QUOTES).'</option>';


标签: php ajax mysqli