I am new to PHP and AJAX, so naked with me on this, I am having trouble using a checkbox to select one or more data fields for PHP / AJAX to process and display. I have PHP / AJAX working fine on my <select> , but as soon as I try to configure the checkbox, all the hellish misses will lose.
I am also very unsure of how to further prevent SQL injection on the site, so if anyone can fill me up a bit more about it, I would GREAT rate it! I read the link that was provided to me and I just donβt understand how bid_param or PDO works.
ajax script: (I cannot insert ajax / js, so I will leave a link to the site live)
Link to agent search page
My php page displaying data:
<div id="bodyA"> <h1>Find a Local OAHU Agent.</h1> </div> <div id="sideB"> <div class="sideHeader"> <em>Advanced Search</em> </div> <form class="formC"> <label for="last">Last Name</label><br /> <select id="last" name="Last_Name" onChange="showUser(this.value)"> <?php include 'datalogin.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> <label for="company">Company</label><br /> <select id="company" name="users" onChange="showUser(this.value)"> <?php include 'datalogin.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> <label for="WorkCity">City</label><br /> <select id="WorkCity" name="WorkCity" onChange="showUser(this.value)" value="city"> <?php include 'datalogin.php'; $result = mysqli_query($con, "SELECT DISTINCT WorkCity FROM `roster` ORDER BY WorkCity ASC;"); echo '<option value="">' . 'Select a City' .'</option>'; while ($row = mysqli_fetch_array($result)) { echo '<option value="'.$row['WorkCity'].'">'.$row['WorkCity'].'</option>'; } ?> </select> <label for="WorkZipCode">Zip Code</label><br /> <select id="WorkZipCode" name="WorkZipCode" onChange="showUser(this.value)"> <?php include 'datalogin.php'; $result = mysqli_query($con, "SELECT DISTINCT WorkZipCode FROM `roster` ORDER BY WorkZipCode + 0 ASC;"); echo '<option value="">' . 'Select a Zip Code' .'</option>'; while ($row = mysqli_fetch_array($result)) { echo '<option value="'.$row['WorkZipCode'].'">'.$row['WorkZipCode'].'</option>'; } ?> </select> <label for="agent">Agent Expertise</label><br /> <label for="ancillary"><input type="checkbox" value="Ancillary" name="Ancillary[]" id="ancillary" />Ancillary</label><br /> <label for="smallgroup"><input type="checkbox" value="Smallgroup" name="Smallgroup[]" id="smallgroup" />Small Group</label><br /> <label for="largegroup"><input type="checkbox" value="LargeGroup" name="LargeGroup[]" id="largegroup" />Large Group</label><br /> <label for="medicare"><input type="checkbox" value="Medicare" name="Medicare[]" id="medicare" />Medicare</label><br /> <label for="longterm"><input type="checkbox" value="LongTerm" name="LongTerm[]" id="longterm" />Long Term Care</label><br /> <label for="individual"><input type="checkbox" value="Individual" name="Individual[]" id="individual" />Individual Plan</label><br /> <label for="tpa"><input type="checkbox" value="TPASelfInsured" name="TPASelfInsured[]" id="tpa" />TPA Self Insured</label><br /> <label for="ppaca"><input type="checkbox" value="CertifiedForPPACA" name="CertifiedForPPACA[]" id="ppaca" />Certified for PPACA</label><br /> </form> </div>
My php page that retrieves the information and puts it in a container on the page:
$q = (isset($_GET['q'])) ? $_GET['q'] : false; // Returns results from user input include 'datalogin.php'; // PHP File to login credentials $sql="SELECT * FROM `roster` WHERE Company = '".$q."' OR Last_Name = '".$q."' OR WorkCity = '".$q."' OR WorkZipCode = '".$q."' ORDER BY Last_Name ASC"; $result = mysqli_query($con,$sql) // Connects to database or die("Error: ".mysqli_error($con)); echo "<h1>" . "Find a Local OAHU Agent." . "</h1>"; while ($row = mysqli_fetch_array($result)) { // Gets results from the database echo "<div class='agentcon'>" . "<span class='agentn'>" . "<strong>".$row['First_Name'] . " " .$row['Last_Name'] . "</strong>" . "</span>" . "<a href=mailto:".$row['Email'] . ">" . "<span class='email'>".$row['Email'] . "</span>" . "</a>" ."<div class='floathr'></div>"; if ($row['Company'] == NULL) { echo "<p>"; } else { echo "<p>" . "<strong>" .$row['Company'] . "</strong>" . "<br>"; } echo $row['WorkAddress1'] . " " .$row['WorkCity'] . "," . " " .$row['WorkStateProvince'] . " " .$row['WorkZipCode'] . "<br>"; if ($row['Work_Phone'] !== NULL) { echo "<strong>" . "Work" . " " . "</strong>" .$row['Work_Phone'] . "<br>"; } if ($row['Fax'] !== NULL) { echo "<strong>" . "Fax" . " " . "</strong>" .$row['Fax'] . "<br>"; } echo "<strong>" . "Agent Expertise:" . "</strong>"; if ($row['Ancillary'] == 1) { echo " " . "Ancillary" . "/"; } if ($row['SmallGroup'] == 1) { echo " " . "Small Group" . "/"; } if ($row['IndividualPlans'] == 1) { echo " " . "Individual Plans" . "/"; } if ($row['LongTermCare'] == 1) { echo " " . "Long Term Care" . "/"; } if ($row['Medicare'] == 1) { echo " " . "Medicare" . "/"; } if ($row['LargeGroup'] == 1) { echo " " . "LargeGroup" . "/"; } if ($row['TPASelfInsured'] == 1) { echo " " . "TPA Self Insured" . "/"; } if ($row['CertifiedForPPACA'] == 1) { echo " " . "Certified For PPACA"; } echo "</p>" . "</div>"; } mysqli_close($con); ?>
I appreciate any help on this topic! Each time I add checkbox values ββto my php file, it finishes displaying everything in the database for all fields in the form.
I am also trying to prevent SQL injection on this, but how can a user do this if I don't have a field that the user can enter into the text?
EDIT So far, I have tried using jQuery to activate checkboxes and then invoke some AJAX. Here is the script I wrote, and it pulls the agent, but not everyone who has this "experience".
$('input').click(function() { $.ajax({ url: "process.php", data: { value: 1}, success: function (data) { $('#bodyA').html(data); } }); });