SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    dynamic select list AJAX and insertion in database table through posting form

    I am wondering to find a solution for my dynamic select list of cities, now I am successfully populated the cities but now unable to post the selected city to database.

    here is php file:

    Code:
    <html>  
    <head>  
    <script type="text/javascript" src="show_cities.js"> 
    </script>   
    </head>  
    <body>  
    <?php   
    session_start;  
    include("conn.php");  
    ?>
      <form enctype='multipart/form-data' action='posting_process.php' method='post'>");  
    <!-- **************************** Country select list ********************** -->  
    Country:   <select name="country" onChange="showCities(this.value)">  
    <?php
     $sql = 'SELECT country_no, country_name FROM country '.'ORDER BY country_no';      
    $rs = mysql_query($sql);      
    echo "<option value='0'>"."Select a Country"."</option>\n  ";
          while($row = mysql_fetch_array($rs))      
    {  echo "<option value=\"".$row['country_no']."\">".$row['country_name']."</option>\n  ";      }  
    ?>  
    </select>  City: 
    <div id="txtCity" class="city_no"> </div> 
    <input type=submit name=action value=Post>
    </form>  
    </body>  
    </html>

    here is javascript: show_cities.js

    Code:
    // JavaScript Document  
    /* <script type="text/javascript"> */  
    function showCities(str)  {  if (str=="")  {  document.getElementById("txtCity").innerHTML="";  return;  }   
    if (window.XMLHttpRequest)  {
    // code for IE7+, Firefox, Chrome, Opera, Safari  
    xmlhttp=new XMLHttpRequest();  }  
    else  {
    // code for IE6, IE5  
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  
    }  
    xmlhttp.onreadystatechange=function()  {  
    if (xmlhttp.readyState==4 && xmlhttp.status==200)  
    {  document.getElementById("txtCity").innerHTML=xmlhttp.responseText;  }  
    }  
    xmlhttp.open("GET","get_cities.php?q="+str,true);  xmlhttp.send();  }  
    /* </script> */

    here is php file: get_cities.php

    Code:
    <?php  
    session_start;  
    //library     
    include("conn.php");  
    $q=$_GET["q"];    
    $sql="SELECT * FROM city WHERE country_no = ".$q;  
    $result = mysql_query($sql);    
    echo "<select name='city'>";    
    while($row = mysql_fetch_array($result)) 
     {    
    echo "<option value=\"".$row['city_no']."\">".$row['city_name'] . "</option>\n";  
    }  
    echo "</select>";    
    ?>
    don't know how to mention the txtcity in my insert statement to insert in database, all the other columns are ok, but city is blank.



    regards:

  2. #2
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    From what I saw in your code, the cities are added in <select name='city'>, so, in your php script you can get the selected option with $_POST['city'] .
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  3. #3
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Please help me, I am wondering for many weeks to solve this problem, but could not.

    here is the insert script:

    [CODE]
    session_start();

    include("conn.php");
    $today = date("Y-m-d");

    $sql_ad_no = mysql_query("select max(ad_no) from ad_info") or die(mysql_error());
    $info_ad_no = mysql_fetch_row( $sql_ad_no );
    $ad_no = $info_ad_no[0];
    $ad_no = $ad_no+1;

    $result = mysql_query("INSERT INTO ad_info (
    ad_no,
    ad_date,
    city,
    country,
    CATEGORY_NO,
    sub_category_no,
    title,
    price,
    description,
    email,
    website,
    user_type,
    phone_no,
    address
    ) VALUES (
    $ad_no,
    '$today',
    '$_POST[city]',
    '$_POST[country]',
    '$_POST[category]',
    '$_POST[sub_category_no]',
    '$_POST[title]',
    '$_POST[price]',
    '$_POST[description]',
    '$_POST[email]',
    '$_POST[website]',
    '$_POST[user_type]',
    '$_POST[phone_no]',
    '$_POST[address]'
    )");

    [CODE]

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,058
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nadeem14375 View Post
    Dear Please help me, I am wondering for many weeks to solve this problem, but could not.
    That is because you are using GET as your Ajax Submission, not POST. So if you change $_POST to $_GET, it will work. However, you should really adjust this further, as you have opened a SQL Injection.

    You should instead change your code to as follows:
    PHP Code:
    session_start();

    include(
    "conn.php");
    $today date("Y-m-d"); 

    $sql_ad_no mysql_query("select max(ad_no) from ad_info") or die(mysql_error());
    $info_ad_no mysql_fetch_row$sql_ad_no );
    $ad_no $info_ad_no[0]; 
    $ad_no $ad_no+1;

    $city mysql_real_escape_string($_GET['city']);
    $country mysql_real_escape_string($_GET['country']);
    $category mysql_real_escape_string($_GET['category']);
    $sub_category_no mysql_real_escape_string($_GET['sub_category_no']);
    $title mysql_real_escape_string($_GET['title']);
    $price mysql_real_escape_string($_GET['price']);
    $description mysql_real_escape_string($_GET['description']);
    $email mysql_real_escape_string($_GET['email']);
    $website mysql_real_escape_string($_GET['website']);
    $user_type mysql_real_escape_string($_GET['user_type']);
    $phone_no mysql_real_escape_string($_GET['phone_no']);
    $address mysql_real_escape_string($_GET['address']);

    $result mysql_query("INSERT INTO ad_info (
    ad_no,
    ad_date,
    city,
    country,
    CATEGORY_NO,
    sub_category_no,
    title,
    price,
    description,
    email,
    website,
    user_type,
    phone_no,
    address
    ) VALUES (
    $ad_no,
    '
    $today',
    '
    $city',
    '
    $country',
    '
    $category',
    '
    $sub_category_no',    
    '
    $title',
    '
    $price',
    '
    $description',
    '
    $email',
    '
    $website',    
    '
    $user_type',
    '
    $phone_no',
    '
    $address'
    )"
    ); 

  5. #5
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear cpradio,

    in my <form > I used the method=post so why I use the get?

    this also not work for me. $city = mysql_real_escape_string($_GET['city']);

    the other values are ok, and can be inserted.

    why this city have problem?

    regards:

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,058
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I misread your original post and the corresponding code. You are correct, your form does use $_POST.

    Can you add the following to your posting_process.php file?
    PHP Code:
    var_dump($_POST); 
    I'd like to see what fields are being sent to it.

  7. #7
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here is the out put and there is no city again.

    array(11) { ["country"]=> string(1) "1" ["title"]=> string(3) "pak" ["price"]=> string(2) "23" ["description"]=> string(2) "23" ["email"]=> string(14) "emai@email.com" ["website"]=> string(10) "www.abc.om" ["user_type"]=> string(1) "1" ["phone_no"]=> string(2) "33" ["address"]=> string(2) "22" ["MAX_FILE_SIZE"]=> string(7) "1000000" ["action"]=> string(4) "Post" }

  8. #8
    SitePoint Member
    Join Date
    Jul 2012
    Location
    Minnesota
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe this has to do with your use of innerHTML to dynamically create your city dropdown. This person had an experience similar to yours:

    http://www.webdeveloper.com/forum/sh...d.php?t=218311

    Try doing some Googling for stuff about innerHTML not passing form fields and you'll find a few more discussions with possible solutions for you. Hope that helps!

  9. #9
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your reply,

    can you guide me to some other option to gain the result, because this a very lengthy discussion / research and I am beginner in web development.

    Please guide me to some other solution.

  10. #10
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the following tutorial help me solve the problem.

    http://www.9lessons.info/2010/08/dyn...box-using.html


Tags for this Thread

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
  •