私はAJAXを初めて使用します
私は何かをしたい
$query="select name from login_master where name=txtName.value();";
//txtName is a name of textbox.
しかし、フォームを送信せずに。私はajaxでこれを行うことができますか?
AJAX では、このクエリを含む PHP ページを呼び出します。PHP ページはクエリを実行し、Javascript が理解できる形式 (おそらく HTML または JSON) で結果をエコーアウトします。
その後、ajax 呼び出しの成功ハンドラーで、返されたデータを処理できます。
また、サーバー側でも注意が必要です。ユーザーによる入力は潜在的に危険である可能性があるためです。mysqli または PDO で準備済みステートメントを使用します。
このようなものが動作するはずです:
<script type="text/javascript">
$(document).ready(function() {
$('#submit-btn').click(function() {
$.ajax({
type:'POST', //POST or GET depending if you want to use _GET or _POST in php
url:'your-page.php', //Effectively the form action, where it goes to.
data:$('#txtName').val(), //The data to send to the page (Also look up form serialise)
success:function(e) {
// On success this fill fire, depends on the return you will use in your page.
}
});
return false;
});
});
</script>
<form>
<input type="text" id="txtName" />
<input type="submit" id="submit-btn" value="Send" />
</form>
次に、あなたの、your-page.php
またはあなたがそれを呼び出すものは何でも$_POST['txtName']
、データベースをピックアップしてクエリを実行します。