2016-08-19 6 views
0

私の最初のフィールド(entrynum)は更新時にMySQLi検索を実行し、残りのフィールドにはレコードが自動入力されます。sqlは私が試しても何のデータも返しません

質問とオリジナルの投稿を更新しました。私はonchangeが正しく発生するフォームフィールドを持っていますが、私が試しても何のデータも返しません。

reg.php

<?php include("process.php"); ?> 

<!doctype html> 
<html> 
<head> 
    <script type="text/javascript" src="js/jquery.js"></script> 
    <script type="text/javascript" src="js/entrynum.js"></script> 
</head> 
<body> 
     <?php 
if (isset($_POST['reg-submit'])) { 
    echo "<p id='notice' style='padding: .5em; border: 2px solid red;'>Entry $entrynum Saved!<br>$timenow on $datenow<br><a href='upload.php' style='font-size:xx-large;'>Upload Pictures</a></p>"; 
} else { 
    echo "<p id='notice' style='display: none; padding: .5em; border: 2px solid red;'></p>"; 
} 
?> 
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
     <fieldset> 
      <legend><h1>Registration</h1></legend> 
      <label for="entrynum">Entry Number</label> 
      <input type="number" pattern="\d*" name="entrynum" id="entrynum" value="" required="true" placeholder="" autofocus onchange="entry_check()" /> 

      <label for="fname">First Name</label> 
      <input type="text" name="fname" id="fname" value="" required="true" placeholder="" list="" style="text-transform:capitalize" onkeyup="javascript:this.value=this.value.charAt(0).toUpperCase() + this.value.substr(1);" /> 

      <label for="lname">Last Name</label> 
      <input type="text" name="lname" id="lname" value="" required="true" placeholder="" list="" style="text-transform:capitalize" onkeyup="javascript:this.value=this.value.charAt(0).toUpperCase() + this.value.substr(1);" /> 

      <input type="submit" name="reg-submit" id="reg-submit" value="Submit" /> 
     </fieldset> 
    </form> 
</body> 
</html> 

process.php

<?php 
include("connect.php"); 
error_reporting(E_ALL); 
ini_set('display_errors', 'On'); 
date_default_timezone_set('US/Central'); 
session_start(); 

$datenow = date("y-m-d"); 
$timenow = date("h:i:sa"); 

// registration 
if (!empty($_POST['reg-submit'])) { 
    $entrynum = $_POST['entrynum']; 

    $_SESSION['entrynum'] = $entrynum; 

    $fname = $_POST['fname']; 
    $lname = $_POST['lname']; 


    $sql = "INSERT INTO HeatWaveData (entrynum, FName, LName) 
     VALUES ('$_POST[entrynum]','$_POST[fname]','$_POST[lname]')"; 

    if (!$db->query($sql)) { die("Error: {$db->errno} : {$db->error}"); } 

} 
?> 

entrynum.js

function entry_check() { 


    var entrynum = $("#entrynum").val(); 

    // Send the AJAX call 
    $.post(
     'entrysearch.php', // TO search.php page 
     {entrynum: entrynum}, // the DATA sent with the request 
     function(data) { // a CALLBACK function 
      if (data == 'none') { // no rows were found or an error occurred 
       document.getElementById("notice").innerHTML = "New Entry!"; 
       document.getElementById("notice").style.display = "block"; 
       return; 
      } else { 
       document.getElementById("notice").innerHTML = "Already Exists!"; 
       document.getElementById("notice").style.display = "block"; 
      } 
      data = JSON.parse(data); // parse the array returned from the server 

      // set the data in the matching fields (could be done manually if needed) 
      // for (var i = 0; i < data.length; i++) { 
      // $("#data" + i).val(data[i]); 
      // } 
     } 
    ); 

} 

entrysearch.php

<?php 
// check post data is received 
if (!isset($_POST['entrynum'])) { 
    echo 'none'; 
    die(); 
} 
// create a prepared statement against sql-injection 
$sql = $db->prepare("SELECT * FROM HeatWaveData WHERE entrynum=%d", $_POST['entrynum']); 
// execute the query (depending on your db class type get the results otherwise) 
$results = $db->query($sql); 
// After this line results should be a matrix (change/add lined as needed) 
// we need the first row returned (probably only 1 was returned...) 
$result = $results[0]; 
// check if a row was returned 
if (!$result) { 
    echo 'none'; 
    die(); 
} 
echo json_encode($result); 
?> 
+0

私が正しく理解していれば、あなたは必要があります(HTTP [イベントonfocusout]://www.w3schools .com/jsref/event_onfocusout.asp)を使用して、[ajax呼び出し](http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp)をあなたにスクリプト(coul'd PHPにする)をそのフィールドの値をデータベースに照会し、有効なデータを返すかどうかをチェックします(他のフィールドのデータに設定されています)(http://www.w3schools.com/jsref/prop_text_value.asp)。それぞれのリンクに例があり、それが役立つことを願っています。 –

答えて

0

これを達成するために必要なことは、必要なデータを検索するスクリプトにajax呼び出しを送る入力フィールドのonChange関数です。
JavaScriptを使用すると、関連するフィールドを更新することができます。

は私がフライ(チェックされていない)の上に、ここでのサンプルコードを書きますが、周りにそれで遊ぶ、それが動作するはずですしています:
注:私は私のコード

foo.htmlという

でのjQueryを使用しています
<script> 
    function input_change() { 
     // Get the value from the input 
     var entrynum = $("#entrynum").val(); 

     // Send the AJAX call 
     $.post(
       'search.php', // TO search.php page 
       {entrynum: entrynum}, // the DATA sent with the request 
       function(data) { // a CALLBACK function 
        if (data == 'none') { // no rows were found or an error occurred 
         return; 
        } 
        data = JSON.parse(data); // parse the array returned from the server 

        // set the data in the matching fields (could be done manually if needed) 
        for (var i = 0; i < data.length; i++) { 
         $("#data" + i).val(data[i]); 
        } 
       } 
     ); 
    } 
</script> 

<input type="number" pattern="\d*" name="entrynum" id="entrynum" value="" required="true" placeholder="" autofocus onchange="input_change()" /> 

<input type="text" name="data0" id="data0"> 
<input type="text" name="data1" id="data1"> 
<input type="text" name="data2" id="data2"> 
<input type="text" name="data3" id="data3"> 

search.php

<?php 
//------------(maybe different depending on your db class type)---------------- 
// begin sesstion and connect to db and stuff 
$db = //get database connection 
// check post data is received 
if (!isset($_POST['entrynum'])) { 
    echo 'none'; 
    die(); 
} 
// create a prepared statement against sql-injection 
$sql = $db->prepare("SELECT * FROM table WHERE entrynum=%d", $_POST['entrynum']); 
// execute the query (depending on your db class type get the results otherwise) 
$results = $db->query($sql); 
// After this line results should be a matrix (change/add lined as needed) 
// we need the first row returned (probably only 1 was returned...) 
$result = $results[0]; 
// check if a row was returned 
if (!$result) { 
    echo 'none'; 
    die(); 
} 
echo json_encode($result); 
?> 
関連する問題