如何使用 PHP 、MySqL 和 JQuery 将动态字段的值插入数据库
How to insert values of dynamic fields into database using PHP ,MySqL and JQuery
我正在尝试在 Jquery 中创建一个国家/地区选择器,它应该使用 PHP 在数据库中提交值。这是脚本:
<script type="text/javascript">
var state_arr=new Array("Jammu and Kashmir","Delhi","Uttar Pradesh","Tamil nadu","Maharashtra","Karnataka","West Bengal","Punjab","Haryana");
var s_a = new Array();
s_a[0]="";
s_a[1]="Srinagar|Jammu";
s_a[2]="New Delhi";
s_a[3]="Lucknow|Kanpur|Ghaziabad|Noida|Varanasi";
s_a[4]="Chennai";
s_a[5]="Mumbai|Pune|Aurangabad|Thane";
s_a[6]="Bangalore|Mysore";
s_a[7]="Kolkata";
s_a[8]="Chandigarh|Mohali";
s_a[9]="Gurgaon|Chandigarh";
function print_state(state){
//given the id of the <select> tag as function argument, it inserts <option> tags
var option_str = document.getElementById(state);
option_str.length=0;
option_str.options[0] = new Option('Select State','');
option_str.selectedIndex = 0;
for (var i=0; i<state_arr.length; i++) {
option_str.options[option_str.length] = new Option(state_arr[i],state_arr[i]);
}
}
function print_city(city, selectedIndex){
var option_str = document.getElementById(city);
option_str.length=0;
option_str.options[0] = new Option('Select City','');
option_str.selectedIndex = 0;
var city_arr = s_a[selectedIndex].split("|");
for (var i=0; i<city_arr.length; i++) {
option_str.options[option_str.length] = new Option(city_arr[i],city_arr[i]);
}
}
这是显示选择器的 PHP 代码:
<?php if (!isset($_POST['submit_val3'])) { ?>
Select the State:
<select onchange="print_city('city',this.selectedIndex);" id="state" name ="state"></select>
<br />
City:
<select name ="city" id ="city"></select>
<script language="javascript">print_state("state");</script>
<input type="submit" name="submit_val3" value="Submit" />
<?php } ?>
这里是PHP提交代码:
if(isset($_POST['submit_val3']))
{
$city=$_POST['city'];
$state=$_POST['state'];
$dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(mysql_error());
mysql_select_db(DB_NAME);
$query="UPDATE register_emp SET state='$state',city='$city' WHERE ID = '" . $_SESSION['ID'] . "'";
mysql_query($query,$dbc) or die('query failed');
mysql_close();
}
我的问题是点击提交按钮后,没有任何值被提交给 database.Where 我错了吗?
请添加表单标签和操作,这就是它不提交到 php 页面的原因。
<?php if (!isset($_POST['submit_val3'])) { ?>
<form method="post" action="yourphppage.php"> <!-- form tag and action page -->
Select the State:
<select onchange="print_city('city',this.selectedIndex);" id="state" name ="state">
City:
<select name ="city" id ="city">
<script language="javascript">print_state("state");
<input type="submit" name="submit_val3" value="Submit" />
</form>
<?php } ?>
我正在尝试在 Jquery 中创建一个国家/地区选择器,它应该使用 PHP 在数据库中提交值。这是脚本:
<script type="text/javascript">
var state_arr=new Array("Jammu and Kashmir","Delhi","Uttar Pradesh","Tamil nadu","Maharashtra","Karnataka","West Bengal","Punjab","Haryana");
var s_a = new Array();
s_a[0]="";
s_a[1]="Srinagar|Jammu";
s_a[2]="New Delhi";
s_a[3]="Lucknow|Kanpur|Ghaziabad|Noida|Varanasi";
s_a[4]="Chennai";
s_a[5]="Mumbai|Pune|Aurangabad|Thane";
s_a[6]="Bangalore|Mysore";
s_a[7]="Kolkata";
s_a[8]="Chandigarh|Mohali";
s_a[9]="Gurgaon|Chandigarh";
function print_state(state){
//given the id of the <select> tag as function argument, it inserts <option> tags
var option_str = document.getElementById(state);
option_str.length=0;
option_str.options[0] = new Option('Select State','');
option_str.selectedIndex = 0;
for (var i=0; i<state_arr.length; i++) {
option_str.options[option_str.length] = new Option(state_arr[i],state_arr[i]);
}
}
function print_city(city, selectedIndex){
var option_str = document.getElementById(city);
option_str.length=0;
option_str.options[0] = new Option('Select City','');
option_str.selectedIndex = 0;
var city_arr = s_a[selectedIndex].split("|");
for (var i=0; i<city_arr.length; i++) {
option_str.options[option_str.length] = new Option(city_arr[i],city_arr[i]);
}
}
这是显示选择器的 PHP 代码:
<?php if (!isset($_POST['submit_val3'])) { ?>
Select the State:
<select onchange="print_city('city',this.selectedIndex);" id="state" name ="state"></select>
<br />
City:
<select name ="city" id ="city"></select>
<script language="javascript">print_state("state");</script>
<input type="submit" name="submit_val3" value="Submit" />
<?php } ?>
这里是PHP提交代码:
if(isset($_POST['submit_val3']))
{
$city=$_POST['city'];
$state=$_POST['state'];
$dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(mysql_error());
mysql_select_db(DB_NAME);
$query="UPDATE register_emp SET state='$state',city='$city' WHERE ID = '" . $_SESSION['ID'] . "'";
mysql_query($query,$dbc) or die('query failed');
mysql_close();
}
我的问题是点击提交按钮后,没有任何值被提交给 database.Where 我错了吗?
请添加表单标签和操作,这就是它不提交到 php 页面的原因。
<?php if (!isset($_POST['submit_val3'])) { ?> <form method="post" action="yourphppage.php"> <!-- form tag and action page --> Select the State: <select onchange="print_city('city',this.selectedIndex);" id="state" name ="state">
City: <select name ="city" id ="city"> <script language="javascript">print_state("state"); <input type="submit" name="submit_val3" value="Submit" /> </form> <?php } ?>