2014-10-07 34 views
0

我想從HTML中的用戶註冊表單獲取數據,然後將數據推送到JSON,然後獲取JSON並存儲到MySQL中。請幫幫我。從HTML表單插入輸入數據到JSON對象,然後將其存儲在MYSQL中

HTML

<form id="myForm" action="userInfo.php" method="post"> 
     <table align="center"> 
      <tr> 
       <td><label for="FirstNameLabel" class="tableproperties">First Name</label></td> 
       <td><input type="text" class="signupTextBoxStyle" name="firstName" placeholder="Enter First Name" id="FirstNameBox" required/></td> 
      </tr> 
      <tr> 
       <td><label for="LastNameLabel" class="tableproperties">Last Name</label></td> 
       <td><input type="text" name="lastName" placeholder="Enter Last Name" id="LastNameBox" class="signupTextBoxStyle" required></td> 
      </tr> 
     <tr> 
      <td><label for="eMailLabel" class="tableproperties">Email</label></td> 
      <td><input type="email" name="email" placeholder="Enter Email" id="eMailBox" class="signupTextBoxStyle" required></td> 
      <td id="emailStatus"></td> 
     </tr> 

     <tr> 
      <td><label for="passwordLabel" class="tableproperties">Password</label></td> 
      <td><input type="password" name="password" placeholder="Enter Password" id="passwordTextbox" maxlength="24" class="signupTextBoxStyle" required></td> 
      <td><i class="fa fa-info-circle infoIcon" title="Password must contain minimum 3 upper case, 2 lower case and 2 special chars"></i></td> 
      <td><progress value="0" max="100" class="progressBar" id="progressStatus"></progress></td> 
      <td id="passwordStrength"></td> 
     </tr> 

     <tr> 
      <td><label for="confirmPasswordLabel" class="tableproperties">Confirm Password</label></td> 
      <td><input type="password" name="confirmpassword" placeholder="Must be same as password" maxlength="24" id="confirmPasswordBox" class="signupTextBoxStyle" required></td> 
      <td id="passwordMismatch"></td> 
     </tr> 

     <tr> 
      <td><label for="dobLabel" class="tableproperties">D.O.B</label></td> 
      <td><input type="date" name="dob" placeholder="Enter D.O.B" id="dobBox" class="signupTextBoxStyle" required></td> 
     </tr> 

     <tr> 
      <td><label for="dobTimeLabel" class="tableproperties">D.O.B with time</label></td> 
      <td><input type="datetime" name="dobTime" placeholder="Enter D.O.B with time" id="dobTimeBox" class="signupTextBoxStyle" required></td> 
     </tr> 

     <tr> 
      <td><label for="localDOBLabel" class="tableproperties">Local D.O.B</label></td> 
      <td><input type="datetime-local" name="localdob" placeholder="Enter Local D.O.B" id="localDobBox" class="signupTextBoxStyle" required></td> 
     </tr> 

     <tr> 
      <td><label for="ssnLabel" class="tableproperties">SSN</label></td> 
      <td><input type="text" name="ssn" placeholder="000-00-0000" id="ssnBox" class="signupTextBoxStyle" required pattern="^(\d{3}-\d{2}-\d{4})$"></td> 
     </tr> 

     <tr> 
      <td><label for="usPhoneNumber" class="tableproperties" >US Phone Number</label></td> 
      <td><input type="text" name="phone" placeholder="000-000-0000" id="usNumberBox" class="signupTextBoxStyle" required></td> 
      <td id="phoneStatus"></td> 
     </tr> 

     <tr> 
      <td><label for="creditLabel" class="tableproperties" id="CreditText">Credit Card Number</label></td> 
      <td><input type="text" name="creditCardNumber" placeholder="Enter Credit Card Number" id="creditBox" class="signupTextBoxStyle" required pattern="^[0-9]{12}(?:[0-9]{4})?$"></td> 
     </tr> 


     <tr> 
      <td colspan='2'> 
       <input type="submit" class="btn btn-primary btn-lg btn-block signupbuttonStyle" id="sub" /> 
       <button type="button" class="btn btn-danger btn-lg btn-block signupbuttonStyle" onclick="location.href = 'index.html';">Cancel</button> 
      </td> 
     </tr> 
     </table> 
    </form> 

PHP(只是爲了測試數據得到保存到MySQL,如果我手動輸入數據)

$json_obj = '{ 
     "jsonFirstName": "Kishan", 
     "jsonLastName": "Kishan", 
     "jsonEmail": "Kishan", 
     "jsonPassword": "Kishan", 
     "jsonDob": "Kishan", 
     "jsonDobTime": "Kishan", 
     "jsonLocaldob": "Kishan", 
     "jsonSsn": "Kishan", 
     "jsonPhonenumber": "Kishan", 
     "jsonCreditcardnumber": "Kishan" 
}'; 

PHP,如果我想(錯誤從表格中獲取數值)

$json_obj = '{ 
     "jsonFirstName": (string) $_POST['firstName'], 
     "jsonLastName": (string) $_POST['lastName'], 
     "jsonEmail": (string) $_POST['email'], 
     "jsonPassword": (string) $_POST['password'], 
     "jsonDob": (string) $_POST['dob'], 
     "jsonDobTime": (string) $_POST['dobTime'], 
     "jsonLocaldob": (string) $_POST['localdob'], 
     "jsonSsn": (string) $_POST['ssn'], 
     "jsonPhonenumber": (string) $_POST['phone'], 
     "jsonCreditcardnumber": (string) $_POST['creditCardNumber'] 
}'; 

錯誤說明
解析錯誤:語法錯誤,意想不到在/Applications/XAMPP/xamppfiles/htdocs/xampp/297test/userInfo.php '姓'(T_STRING)上線的PHP 19

REST代碼
$ result = json_decode($ json_obj);

$firstname = $result->jsonFirstName; 
$lastname = $result->jsonLastName; 
$email = $result->jsonEmail; 
$password = $result->jsonPassword; 
$dob = $result->jsonDob; 
$dobTime = $result->jsonDobTime; 
$localdob = $result->jsonLocaldob; 
$ssn = $result->jsonSsn; 
$phonenumber = $result->jsonPhonenumber; 
$creditcardnumber = $result->jsonCreditcardnumber; 


if(mysql_query("INSERT INTO user VALUES('$firstname', '$lastname', '$email', '$password', '$dob', '$dobTime', '$localdob', '$ssn','$phonenumber','$creditcardnumber')")){ 
    echo "Successfully Inserted"; 
} 

else 
    echo "Fail to Insert"; 
+2

你爲什麼要在json中轉換它? – 2014-10-07 06:10:42

+0

我不明白你爲什麼需要將POST變量放入json字符串中,然後解碼,然後再次使用它。它沒有意義 – Ghost 2014-10-07 06:10:58

+0

$ json_obj =「{'jsonFirstName':」。 mysqli_real_escape_string($ _ POST ['firstName']。「)} - 或者更好的是,使用準備好的語句... – FAS 2014-10-07 06:13:29

回答

1

直接通過串聯創建JSON字符串很難,因爲引號,換行符等等。

相反,創造價值的數組,編碼成JSON字符串json_encode

$values = array(
    "jsonFirstName" =>  $_POST['firstName'], 
    "jsonLastName" =>   $_POST['lastName'], 
    "jsonEmail" =>   $_POST['email'], 
    "jsonPassword" =>   $_POST['password'], 
    "jsonDob" =>    $_POST['dob'], 
    "jsonDobTime" =>   $_POST['dobTime'], 
    "jsonLocaldob" =>   $_POST['localdob'], 
    "jsonSsn" =>    $_POST['ssn'], 
    "jsonPhonenumber" =>  $_POST['phone'], 
    "jsonCreditcardnumber" => $_POST['creditCardNumber'] 
); 

$json_obj = json_encode($values); 

另外,你可以這樣做:

$json_obj = json_encode($_POST); 

然後,您將得到每一個JSON對象索引$_POST。唯一的區別是,你不能像在你的例子中那樣重命名你的字段。

+0

謝謝...迄今它的gng on gud ....當我回顯$ json_obj ..我得到下面的結果({「 jsonFirstName 「:」 userfirst」, 「jsonLastName」: 「userlast」, 「jsonEmail」: 「[email protected]」, 「jsonPassword」: 「QWERqwer @#$」, 「jsonDob!」: 「2014年1月1日」 「jsonDobTime」: 「dobtime」, 「jsonLocaldob」: 「2014-01-01T01:00」, 「jsonSsn」: 「123-12-1234」, 「jsonPhonenumber」: 「123-123-1234」, 「jsonCreditcardnumber」 :「123412341234」})...你能請指導我如何在mySQl服務器中逐一存儲這些值...這將實際上解決我的問題.. – 2014-10-08 01:50:26

+0

它的工作感謝很多fabian ... – 2014-10-08 03:04:47

0

不知道你爲什麼這樣做,但是解析錯誤是因爲你使用的是單引號打開字符串,然後又使用該挑數組索引。

變化$_POST['firstName']用雙引號如$_POST["firstName"]

+0

它stil不wrk ...這個錯誤進來注意:試圖獲取非對象的屬性在/ Applications/XAMPP/xamppfiles /htdocs/xampp/297test/userInfo.php on line 35「$ firstname = $ result-> jsonFirstName;」 - >這行指出 – 2014-10-07 06:18:03

0

編輯: 嘗試這樣做。

$array = (
     "jsonFirstName" => $_POST['firstName'], 
     "jsonLastName" => $_POST['lastName'], 
     "jsonEmail" => $_POST['email'], 
     "jsonPassword" => $_POST['password'], 
     "jsonDob" => $_POST['dob'], 
     "jsonDobTime" => $_POST['dobTime'], 
     "jsonLocaldob" => $_POST['localdob'], 
     "jsonSsn" => $_POST['ssn'], 
     "jsonPhonenumber" => $_POST['phone'], 
     "jsonCreditcardnumber" => $_POST['creditCardNumber'] 
); 

$json = json_encode($array); 
+0

仍然沒有運氣..現在的錯誤是「解析錯誤:語法錯誤,第18行中的/Applications/XAMPP/xamppfiles/htdocs/xampp/297test/userInfo.php中出現意外的'{' – 2014-10-07 06:23:50

+0

看看我編輯的問題 – 2014-10-07 06:32:33

+0

解析錯誤:語法錯誤,意外'=>'(T_DOUBLE_ARROW)in/Applications/XAMPP/xamppfiles/htdocs/xampp/297test/userInfo.php ..我得到這個新的錯誤:( – 2014-10-07 06:43:14

0

,因爲你是在一個錯誤的方式創建JSON對象了POST變量你得到一個錯誤。在你的陳述中引用確實存在問題。它應該是這樣的:

$json_obj = '{ 
    "jsonFirstName": "'.(string) $_POST['firstName'].'", 
    "jsonLastName": "'.(string) $_POST['lastName'].'", 
    "jsonEmail": "'.(string) $_POST['email'].'", 
    "jsonPassword": "'.(string) $_POST['password'].'", 
    "jsonDob": "'.(string) $_POST['dob'].'", 
    "jsonDobTime": "'.(string) $_POST['dobTime'].'", 
    "jsonLocaldob": "'.(string) $_POST['localdob'].'", 
    "jsonSsn": "'.(string) $_POST['ssn'].'", 
    "jsonPhonenumber": "'.(string) $_POST['phone'].'", 
    "jsonCreditcardnumber": "'.(string) $_POST['creditCardNumber'].'" 
    }'; 

如果我只包括沒有雙引號的變量而構建JSON對象,我得到這樣的:

$name = "Foo"; 
    $json_obj = '{"firstName" :'.$name.'}'; 

//gets expanded to $json_obj = {"firstname" : Foo} 
//Whereas it should be $json_obj = {"firstname" :"Foo"} 

正是因爲缺少雙引號的,您無法首先創建JSON對象,因此當您嘗試對其進行解碼並進一步訪問某個屬性時,它會給出錯誤提示:

Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/xampp/297test/userInfo.php on line 35 

試試我給的代碼片段,它應該有希望幫助你。

相關問題