简体   繁体   English

Javascript没有将值传递到我的PHP代码中运行

[英]Javascript is not passing in the values into my php code to run

when i run the form and the sql using action="addCustomer.php" it works fine but when i am using the js the data is not passing through and i am not getting an error in the console and it says that it is picking up the variables in the timeline from the original page but the js is not? 当我运行表格和使用action="addCustomer.php"的sql时,它工作正常,但是当我使用js时,数据未通过,并且控制台中未出现错误,它表示正在接听来自原始页面的时间轴中的变量,但是js不是吗? Can anyone see why? 谁能看到原因?

I am using the following form: 我正在使用以下形式:

  <form id="dataForm" method="GET">
  <div>
    <h2 id="formheader">Add Order Details:</h2>
    <label>First Name:</label>
    <input class="inputForm" id="inputFirstName" type="text" name="firstname">
  </div>
  <div>
    <label>Last Name:</label>
    <input class="inputForm" id="inputLastName"  type="text" name="lastname">
  </div>
  <div>
    <label>Address:  </label>
    <input class="inputForm" id="inputAdress" type="text" name="address">
  </div>
  <div>
    <label>Post Code:</label>
    <input class="inputForm" id="inputPostcode" type="text" name="postcode">
  </div>
  <div>
  <section class="inputForm">
  <label>Delievery Type:</label>
  <select name="deliverytype">
  <option ="3-5 Days">3-5 Days</option>
  <option ="Next Day">Next Day</option>
  </select> 
  </section> 
  <input type="hidden" id="calc" value="" name="calc">
  </div>
  <div id="theSubmit">
      <button id="checkoutButton">Submit</button>
    </div>
  </form>

and the following bit of js: 和下面的js:

function addCustomer(){
  var xmlhttp = new XMLHttpRequest();
  var firstName = document.getElementById("inputFirstName").value;
  var lastName = document.getElementById("inputLastName").value;
  var address = document.getElementById("inputAdress").value;
  var postcode = document.getElementById("inputPostcode").value;
  var delievery = document.getElementById("deliverytype").value;
  if(firstName != "" && lastName != "" && address !="" && postcode !=""){
    var url = "addCustomer.php?FIRSTNAME=" + firstName + "&LASTNAME=" + lastName + "&ADDRESS=" + address + "&POST_CODE=" + postcode + "&DELIVERY_TYPE=" + delievery;

    xmlhttp.open("GET", url, false);
    xmlhttp.send();
     }
   else{
    alert("enter Some Data");
   }
}

submitButton = document.getElementById("checkoutButton");
submitButton.addEventListener("click", addCustomer);

and the following php sql code: 和以下php sql代码:

$name = $_GET['firstname'];
     $lastname = $_GET['lastname'];
     $userAddress = $_GET['address'];
     $userPostCode = $_GET['postcode'];
     $delivery = $_GET['deliverytype'];
     $totalCost = $_GET['calc'];

     if($name !="" && $lastname !="" && $userAddress !="" && $userPostCode !="" ){
     $sql = "INSERT INTO USERS (FIRSTNAME, SECONDNAME, ADDRESS, POST_CODE, DELIVERY_TYPE) VALUES ('$name', '$lastname', '$userAddress', '$userPostCode', '$delivery') ";
     $conn->exec($sql);
     echo "worked";
     }

Try to send variables in lowercase: 尝试以小写形式发送变量:

var url = "addcustomer.php?firstname=" + firstName + "&lastname=" + lastName + "&address=" + address + "&post_code=" + postcode + "&delivery_type=" + delievery;

Any way you are just sending the variable any changes should happen in database not in console log of your browser And PLEASE read about SQL Injection ... 您以任何方式发送变量的任何更改都应该在数据库中发生,而不是在浏览器的控制台日志中发生,请阅读有关SQL注入的信息 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM