[英]How can I see $_POST variable from a form using JQuery?
I am trying to obtain the $_POST variable generated from a login form using JQuery. 我试图获取使用JQuery从登录表单生成的$ _POST变量。 My web page has three sections Header, Navigation and Main. 我的网页包含三个部分:标题,导航和主要。 "Main" contains the form and should be changed depending on the results of the input to the form, (login validation). “主”包含表单,应根据表单输入的结果进行更改(登录验证)。 I can get the "main" section to change when thr form is submitted but cannot obtain the $_POST variables created by the form's tags. 我可以在提交thr表单时更改“ main”部分,但无法获取由表单标签创建的$ _POST变量。
I suspect I need to use ajax in order to do this but I am totally new to ajax and can't find a good and simple example to follow. 我怀疑我需要使用ajax来执行此操作,但是我对ajax完全陌生,因此找不到可以遵循的好而简单的示例。
Help and suggestions as to how I can get this working would be greatly appreciated. 对于如何使我工作的帮助和建议将不胜感激。
Here are the test files that I am using to try this out. 这是我正在尝试使用的测试文件。 (The array dump in TEST_1.php is always returned empty) (TEST_1.php中的数组转储始终返回空)
TEST.php TEST.php
<?php
session_start();
echo "<!DOCTYPE html><html lang='en'><head>";
echo "<script language='JavaScript1.1' src='scripts/KSG.js'></script>";
echo "<script type='text/javascript' charset='utf-8'>
function valid_mem(){
if (document.form_1.member.value.length === 0){
req('Username.');
return false;
}else{
if (document.form_1.mpass.value.length === 0){
req('Password');
return false;
}return true;
}}
</script>";
echo "<script src='js/jquery-1.8.0.min.js'></script>";
echo "</head>";
echo "<body style='background-color:#eeeecc'>";
echo " <div style='background-color:#6495ed; height: 80px;'>";
echo " <div class='col-md-12 text-center'><b>HEADER</b> div</div>";
echo " </div>";
echo " <div id='mainx' style='background-color:#cccccc; height: 600px;'>";
require ('TEST_0.php');
echo " </div></body></html>";
?>
TEST_0.php TEST_0.php
<?php
echo "<script type='text/javascript' charset='utf-8'>
$(document).ready(function(){
$('#form_1').submit(function(e){
e.preventDefault();
var z = valid_mem();
if(z){
var x = $('#form_1').attr('action');
alert('debug_1: '+x);
console.log(x);
$('#mainx').load(x);
}
});
});
</script>";
echo "<form id='form_1' name='form_1' action='TEST_1.php' method='post'>";
echo "<b>MAIN</b> div<br>A test form using submit<br><br>";
echo "Member: <input type='text' name='member' > ";
echo "Password: <input type='text' name='mpass' >";
echo "<button id='but1' type='submit' >OK</button>";
echo "</form>";
?>
TEST_1.php TEST_1.php
<?php
echo " THIS IS A DUMMY PAGE: TEST 1<br><br>";
echo "<pre>";var_dump($_POST);echo "</pre>";
?>
First of all, you seem new to PHP in general. 首先,一般来说,PHP似乎是新手。 I recommend you read the "getting started" section on php.net (specifically, you shouldn't be "echo"ing everything, it makes your code hard to read - just leave it outside the tags). 我建议您阅读php.net上的“入门”部分(特别是,您不应该“回声”所有内容,它会使您的代码难以阅读-只需将其放在标签之外即可)。
Second, as PHP variables are server side, you cannot access them directly via JS. 其次,由于PHP变量是服务器端的,因此您无法直接通过JS访问它们。 You have the following options: 您有以下选择:
As to AJAX , it is generally a great way to get data from the server side to the client side dynamically, but in this case it would be unneccesarily complicated, as $_POST is only available to the page the form is sent to. 对于AJAX来说 ,通常是从服务器端动态获取数据到客户端的一种好方法,但是在这种情况下,它将变得不必要地复杂,因为$ _POST仅可用于将表单发送到的页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.