SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Submission form with Ajax values?

    I am trying to create website with PHP,MySQL,Ajax.In my form,the values of sub-category select box are populated according to the value in the category select box by calling ajax function showsubCategory(str) ,below I am showing some of the code:

    Ajax code:
    Code:
    <script type="text/javascript">
        var xmlHttp
        function showsubCategory(str)
    	{
    	   xmlHttp=GetXmlHttpObject()
    						
    	  if (xmlHttp==null)
    	     {
    		alert ("HTTP Request is not supported by browser")
    		return
    	    } 
         var url="SubCategory_add.php"
         url=url+"?cat="+str
         url=url+"&rid="+Math.random()
         xmlHttp.onreadystatechange=stateChanged
         xmlHttp.open("GET",url,true)
         xmlHttp.send(null)
       }
    			
      function stateChanged()
       { 
    	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    	 { 
               document.getElementById("Sub-  Category").innerHTML=xmlHttp.responseText
             } 
       } 
    
      function GetXmlHttpObject()
        {
    	var objXMLHttp=null	
    	if (window.XMLHttpRequest)
    	 {
    		objXMLHttp=new XMLHttpRequest()
    	 }
    	else if (window.ActiveXObject)
    	 {	
    		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")	
    	 }	
    	 return objXMLHttp
         }
    			
    </script>
    classifiedform.php code:
    PHP Code:
    <form action="classifiedform_add.php" method="post"> 
         <tr>
             <td style="color:#0099CC;width:250px"><div align="left">Select Category</div>
             </td>
                          
              <td >
            <select name="select" id="select" style="width:150px"   onchange="[B]showsubCategory[/B](this.value);" >
                         <option value="" selected="selected">Select Category</option>
                <?php
                    
    require_once('config.php');
                                
                    
    //Connect to mysql server
                   
    $link mysql_connect(DB_HOSTDB_USERDB_PASSWORD);
                   if(!
    $link
                                 {
                    die(
    'Failed to connect to server: ' mysql_error());
                     }
                                
                   
    //Select database
                  
    $db mysql_select_db(DB_DATABASE);
                  if(!
    $db
                                 {
                    die(
    "Unable to select database");
                     }
                                
                                    
                
    // Form a query to populate the category combo-box
                    
    $query "SELECT * FROM tbl_catagories;";
                                
                
    // Successful query?
                    
    if($result mysql_query($query))  
                  {
                    
    // If there are results returned, create options
                          
    if($success mysql_num_rows($result) > 0
                      {
                     
    // For each item in the results...
                    
    while ($row mysql_fetch_array($result))
                    
    // Add a new option to the combo-box
                    
    echo "<option value=\"$row[category_name]\">$row[category_name]</option>\n";

                          }
                       else
                          {    
                                        echo 
    "No results found.";
                   }
        
                   
    ?>
                            </select>
                          </td>
                        </tr>
                <tr>
                          <td style="color:#0099CC;width:250px;">
                             <div align="left">
                                   Select Sub-Category
                             </div>
                          </td>
                          <td>
                             <div id="Sub-Category">
                             <select name="SubCategoryName" id="SubCategoryName" style="width:150px">
                                  <option value="" selected="selected">
                                  Select Sub-Category</option>
                            </select>
                </div>
                          </td>
                        </tr>
    </form>
    classifiedform_add.php code:
    PHP Code:
    <?php
                                
                            
    //Function to sanitize values received from the form. Prevents SQL injection
    function clean($str
    {
          
    $str = @trim($str);
                                            if(
    get_magic_quotes_gpc()) 
            {
             
    $str stripslashes($str);
        }
        return 
    mysql_real_escape_string($str);
    }
        
    //values received from form
           
    $category clean($_POST['Categoryname']);
           
    $subcategory clean($_POST['SubCategoryName']);

        
    //MySQL code                         
           
    ........................
    ?>
    In classifiedform_add.php I am not getting the value of Sub-Category from the select box of subcategories which is populated by ajax function showsubCategory(str) to submit into MySQL database,when it is submitted from classifiedform.php.Any kind of help in getting the value of Sub-Category will be appreciated.

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by elearnindia View Post
    In classifiedform_add.php I am not getting the value of Sub-Category from the select box of subcategories which is populated by ajax function
    If it's a pure injection of innerHTML then the elements added do not become part of the form.
    Use the downloaded data to append options using the Option() constructor.
    Tab-indentation is a crime against humanity.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •