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:
<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>\
";
while($row = mysql_fetch_array($rs))
{ echo "<option value=\\"".$row['country_no']."\\">".$row['country_name']."</option>\
"; }
?>
</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
// 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
<?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>\
";
}
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:
MarPlo
July 31, 2012, 12:18pm
2
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’] .
Dear Please help me, I am wondering for many weeks to solve this problem, but could not.
here is the insert script:
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]'
)");
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:
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'
)");
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:
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?
var_dump($_POST);
I’d like to see what fields are being sent to it.
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” }
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/showthread.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!
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.
the following tutorial help me solve the problem.