Jump to content

Passing Checkbox parameter to MySQL through PHP


bigashish123

Recommended Posts

To all AJAX Gurus!

 

I am trying to create a small ajax script that will allow me to select one or multiple check boxes and pass on their value (variable).

 

Once I am on the PHP page, I am having trouble trying to verify if the checkbox is checked or not.  Or I might be having a problem with my AJAX script.  I am not sure at this point!!  ???

 

Here is the initially AJAX script along with the initial HTML page:

 

<html>
<body>

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
   var ajaxRequest;  // The variable that makes Ajax possible!
   
   try{
      // Opera 8.0+, Firefox, Safari
      ajaxRequest = new XMLHttpRequest();
   } catch (e){
      // Internet Explorer Browsers
      try{
         ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
         }
      }
   }
   // Create a function that will receive data sent from the server
   ajaxRequest.onreadystatechange = function(){
      if(ajaxRequest.readyState == 4){
         var ajaxDisplay = document.getElementById('ajaxDiv');
         ajaxDisplay.innerHTML = ajaxRequest.responseText;
      }
   }
   var age = document.getElementById('age').value;
   var wpm = document.getElementById('wpm').value;
   var sex = document.getElementById('sex').value;
  var colord = document.getElementById('colord').value;
   var queryString = "?age=" + age + "&wpm=" + wpm + "&sex=" + sex + "&colord=" + colord;
   ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
   ajaxRequest.send(null); 
}

//-->
</script>



<form name='myForm'>
Max Age: <input type='text' id='age' /> <br />
Max WPM: <input type='text' id='wpm' />
<br />
Sex: <select id='sex'>
<option value='m'>m</option>
<option value='f'>f</option>
</select>
<BR>
Color: 
 D <input type="checkbox" name="colord" id="colord" value="D"/>
 E <input type="checkbox" name="colore" value="E"/>
 F <input type="checkbox" name="colorf" value="F"/>
 G <input type="checkbox" name="colorg" value="G"/>
 H <input type="checkbox" name="colorh" value="H"/>
 I <input type="checkbox" name="colori" value="I"/>
 J <input type="checkbox" name="colorj" value="J"/>
<BR>
<input type='button' onclick='ajaxFunction()' value='Query MySQL' />
</form>
<div id='ajaxDiv'>Your result will display here</div>
</body>
</html>

 

I am trying to experiment with the "colord" checkbox only. The two text fields and the drop down list work fine but the chechbox functionality does not. 

 

Please be kind and detailed in your critique of my coding. I am definitely a newbie at this!

 

Thank you for all your help!!

 

Here is the PHP Code:

 

<?php
$dbhost = "IPAddress";
$dbuser = "username";
$dbpass = "password";
$dbname = "dbname";
   //Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
   //Select Database
mysql_select_db($dbname) or die(mysql_error());
   // Retrieve data from Query String
$age = $_GET['age'];
$sex = $_GET['sex'];
$wpm = $_GET['wpm'];
$colord = $_GET["colord"];
   // Escape User Input to help prevent SQL Injection
$age = mysql_real_escape_string($age);
$sex = mysql_real_escape_string($sex);
$wpm = mysql_real_escape_string($wpm);
$colord = mysql_real_escape_string($colord);
   //build query
$query = "SELECT * FROM ajax_example WHERE ae_sex = '$sex'";
if(is_numeric($age))
   $query .= " AND ae_age <= $age";
if(is_numeric($wpm))
   $query .= " AND ae_wpm <= $wpm";
if($colord.checked = true)
   $query .= " AND ae_color = $colord";
   //Execute query
$qry_result = mysql_query($query) or die(mysql_error());

   //Build Result String
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Age</th>";
$display_string .= "<th>Sex</th>";
$display_string .= "<th>WPM</th>";
$display_string .= "<th>Diamond Color</th>";
$display_string .= "</tr>";

   // Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
   $display_string .= "<tr>";
   $display_string .= "<td>$row[ae_name]</td>";
   $display_string .= "<td>$row[ae_age]</td>";
   $display_string .= "<td>$row[ae_sex]</td>";
   $display_string .= "<td>$row[ae_wpm]</td>";
   $display_string .= "<td>$row[ae_color]</td>";
   $display_string .= "</tr>";
   
}
echo "Query: " . $query . "<br />";
$display_string .= "</table>";
echo $display_string;
?>

Link to comment
Share on other sites

Hi

 

Firstly, on your php code, you have = instead of == on the $colord.check = true area.  It needs 2 = signs for comparison.

 

Secondly, to pass multiple values for the check boxes, you will have to iterate through them to see which are checked and then append to the url using Javascript.

corbin's response to my thread shows this.

 

The idea is that the url for sending an array can look like this:

http://www.yoursite.com/somepage.php?age=21&wpm=90&sex=m&color[]=D&color[]=F

 

This way, the $_GET['color'] variable will contain and array holding "D" and "F" as separate elements.

 

It looks like the easiest way to handle this with Javascript is to name the checkbox name attribute the same (such as <input type="checkbox" name="color"> and only change the value attribute values.  Then you can iterate through them checking their value and if so, append the url to look like the one above.  (example of iterating)

 

Hope that helps.

 

 

ps> be more patient

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.