不能$ _GET或$ _POST动态下拉变量

时间:2017-06-28 18:06:01

标签: javascript php jquery mysql

的JavaScript

// JQuery script is on ajax.php page
// JQUERY PIECE TO PROCESS STATE AND CITY VALUES 
// BASED ON COUNTRY SELECTION

function change_CountryNo() {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.open("GET", "ajax.php?CountryNo=" + document.getElementById("CountryNodd").value, false);
  xmlhttp.send(null);
  document.getElementById("StateID").innerHTML = xmlhttp.responseText;


  if (document.getElementById("CountryNodd").value == "CountryNo") {
    document.getElementById("CityID").innerHTML = "<select><option>City</option></select>";

  }
}

function change_StateID() {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.open("GET", "ajax.php?StateID=" + document.getElementById("StateIDdd").value, false);
  xmlhttp.send(null);
  document.getElementById("CityID").innerHTML = xmlhttp.responseText;

  if (getElementById("StateIDdd").value == "StateID") {
    document.getElementById("CityID").innerHTML = "<select><option>City</option></select>";

  }
}


<!DOCTYPE html>
<html>

<head>

  <!-- index.php -->

  <!-- THIS CODE PROVIDES A DROP DOWN FORM
FOR USER TO SELECT COUNTRY THEN STATE THEN CITY
A JQUERY SCRIPT PROCESSES THE COUNTRY VALUE
TO PRODUCE THE APPROPRIATE STATES AND 
PROCESSES THE STATE VALUE TO PRODUCE
THE APPROPRIATE CITIES -->


  <title>Country, State, City Selection</title>
  <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="css/styles.css">

</head>

<body>


  <!-- THESE FILES ARE DATABASE CONNECTION AND FUNCTIONS
FOR VARIOUS LOGIN, USER SESSION VARIABLES TO IDENTIFY
THE CONTACT NO OF USER UPDATING THEIR LOCATION -->

  <?php include("includes/header.php") ?>

  <?php include("includes/nav.php") ?>



  <?php //Check user's login status 
if (logged_in() === false) {

        echo "Redirecting...";
        redirect("../index.html"); 
      }
?>

  <!-- responsive setup for form -->
  <div class="row">
    <div class="col-lg-6 col-lg-offset-3">
    </div>
  </div>
  <!-- end of class row div -->
  <div class="row">
    <div class="col-md-6 col-md-offset-3">
      <div class="panel panel-login">
        <div class="panel-heading">
          <div class="row">
            <div class="col-xs-12">
              <div class="col-xs-6">
              </div>
              <div class="col-xs-6">
                <a href="Country-State-City.php" class="active" id="">Country-State-City Selection</a>
              </div>
            </div>
            <hr>
          </div>
          <!-- end of class row div -->
          <div class="panel-body">
            <div class="row col-md-12" style="text-align: center">
              <form method="POST" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]);?>" autocomplete="off">
                <div id="CountryNo" class="col-xs-3 form-group" style="font-size: 75%">
                  <select id="CountryNodd" name="CountryNodd" onChange="change_CountryNo()" class="form-control selectpicker" style="width:100%;">
                      <option>Country</option>
                      <?php
                          $res=mysqli_query($con, "SELECT * FROM countries ORDER BY Country_Descrip");
                          while($row=mysqli_fetch_array($res))
                          {
                      ?>
                          <option value="<?php echo $row["CountryNo"]; ?>"><?php echo $row["Country_Descrip"]; ?></option>
                          <?php $CountryNodd = $_POST["CountryNo"]; ?>
                      <?php                }

                      ?>
                    </select>
                </div>

                <div id="StateID" class="col-xs-3 form-group" style="font-size: 75%">
                  <select id="StateIDdd" name="StateID" class="form-control selectpicker" style="width:100%;">
                  <option>Product</option>
                  </select>
                </div>
                <div id="CityID" class="col-xs-3 form-group" style="font-size: 75%">
                  <select id="CityIDdd" name="CityIDdd" class="form-control selectpicker" style="width:100%;">
                <option>Brand</option>
                </select>
                </div>
                <div class="form-group">
                  <img src="img\..." class="img-responsive" alt="Country-State-City Image" width="100%" height="auto">
                  <div class="col-lg-12 text-center">
                    <br>
                    <input type="submit" name="reset" id="reset" tabindex="3" class="form-control btn btn-register" value="Reset Country-State-City Selections" required>
                  </div>
                </div>
                <div class="form-group">
                  <div class="col-lg-12 text-center">
                    <br>
                    <input type="submit" name="Update My Country-State-City" id="update" tabindex="4" class="form-control btn btn-register" value="Update My Country-State-City" required>
                  </div>
                </div>
              </form>
              <!-- end of form  -->
            </div>
            <!-- end of row col-md-12 div -->
          </div>
          <!-- end of panel-body div -->
        </div>
        <!-- end of panel-heading div -->
      </div>
      <!-- end of panel-login div -->
    </div>
    <!-- end of col-md-6 col-md-offset-3 div -->
  </div>
  <!-- end of class row div -->



  <!-- ajax.php -->

  <!DOCTYPE HTML>
  <html>

  <head>
    <title>ajax.php</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>


    <?php

//THIS FILE PROCESSES THE JQUERY SELECT BASED ON COUNTRY INPUT AND
//THE STATE INPUT


// Turn off error reporting
error_reporting(0);

$con=mysqli_connect('localhost', 'root', '',  'contact_info');
mysqli_set_charset($con,"utf8");
if (!$con) {
    die('Could not connect: ' . mysqli_error());
}


// Get the Country and State Values User Selects

$CountryNo=$_GET["CountryNo"];
$StateID=$_GET["StateID"];


//Check user selection for country and list states

if($CountryNo!="Country")
{
    $query="SELECT State_ID, StateCountry_ID, State_Description FROM states WHERE StateCountry_ID=$CountryNo ORDER BY State_Description";
    $result=mysqli_query($con, $query);
    echo "<select id='StateIDdd' onchange='change_StateID()' selected>";
    echo "<option>"; echo "State"; echo "</option>";
    while($row=mysqli_fetch_array($result))
    {
        echo "<option value='$row[State_ID]'>"; echo $row["State_Description"]; echo "</option>";

    }
    echo "</select>";

} 

//Check user selection for state and list cities

if($StateID!="State")
{
      $query="SELECT CityID, City_Name, CountryNo, CityState_ID FROM cities WHERE CityState_ID=$StateID ORDER BY City_Name";
      $result=mysqli_query($con, $query);
      echo "<select>";

      while($row=mysqli_fetch_array($result))
      {
          echo "<option value='$row[CityID]' selected>"; echo $row["City_Name"]; echo "</option>";

      }
      echo "</select>";


} 
?>


      <?php include("ajax2.php") //Go to post to MySQL processing 
?>

  </head>

  </html>




  <!-- ajax2.php -->


  <!-- THIS FILE POSTS THE COUNTRY, STATE, CITY VALUES TO
MYSQLI AND UPDATES THE USER'S CONTACT LOCATION -->


  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>


  <?php include("includes/header.php") ?>

  <?php include("includes/nav.php") ?>


  <?
// Turn off error reporting
error_reporting(0);

$con=mysqli_connect('localhost', 'root', '',  'contact_info');
mysqli_set_charset($con,"utf8");
if (!$con) {
    die('Could not connect: ' . mysqli_error());
}

?>


    <?
// Ensure user is logged in to get values
// for ContactNo

php  if (logged_in() === false) {

        echo "Redirecting...";
        redirect("../index.html");
      } 

    //store country, state, city selection and update contact's location 

        $CountryNo      = $_POST['CountryNodd'];
        $StateID        = $_POST['StateIDdd'];
        $CityID         = $_POST['CityIDdd'];


    //Update MySQL
        $sql = "UPDATE contact_location SET CountryNodd=$CountryNo, StateID=$StateID, CityID=$CityID";
        $sql.= "WHERE ContactNo=$ContactNo"; //Contact info is from contact table accessed via function include in header.php

        $res_update = mysqli_query($con,$sql);
          if($res_update) {
                echo "Location updated successfully";
                }
                else {
                    echo "Not working...";
                }


?>

我已阅读相关问题/回复,但没有解决我的问题。   我是jQuery&amp;的新手使用MySQL后端进行PHP编程。

在用户处创建了一个3级下拉(国家/州 - 城市)表单 可以选择国家 - 州 - 城市价值,这将更新 MySQL的。

虽然我可以GET和POST国家变量,但我一直都是 国家和城市不成功。我可以看到正确的价值 State变量,但我没有看到City变量的值 此外,我不能POST状态或城市变量:我得到一个 与每个变量关联的变量的“索引未定义”错误。

1 个答案:

答案 0 :(得分:0)

已解决:POST执行发生在JavaScript操作之前,这是因为PHP,服务器端,在Java之前执行,客户端。我在城市选择发生时将MySQL更新代码移动到PHP脚本中。

我不会提供代码,因为到目前为止我收到的评论表明我的文档需要工作。当然感谢反馈并将采取措施改进未来的帖子。谢谢!