[英]Transfer form data from ajax to php
How can I transfer form data from ajax to php? 如何将表单数据从ajax传输到php? But not via POST (means in the URL).
但不是通过POST(在URL中的意思)。
patient_anlegen.php patient_anlegen.php
<form id="form_patienten_anlegen" name="form_patienten_anlegen">
<table class="table_patienten_anlegen">
<tr>
<td>Anrede:</td>
<td>
<select id="termin_anrede" name="termin_anrede">
<option value=0>Bitte auswählen</option>
<option value="Frau">Frau</option>
<option value="Herr">Herr</option>
</select>
</td>
</tr>
<tr>
<td>Vorname:</td>
<td><input type="text" size="30" name="termin_vorname" id="termin_vorname" ><div class="error" id="termin_vornamefehler"></div></td>
</tr>
<tr>
<td>Nachname:</td>
<td><input type="text" size="30" name="termin_nachname" id="termin_nachname"><div class="error" id="termin_nachnamefehler"></div></td>
</tr>
</form>
My patient.js 我的病人.js
$(document).on('click', '#submit_patienten_anlegen', function() {
alert("Test");
$.ajax({url: 'func/patienten.php',
data: $('#form_patienten_anlegen').serialize(),
parameters: {
action: 'patienten_anlegen'
},
type: 'post',
beforeSend: function() {
},
complete: function() {
},
success: function (result) {
var response = JSON.parse(result);
if(response.status) {
alert(response.status);
} else {
alert(response.message);
}
},
error: function (request,error) {
alert('Fehler!');
}
});
});
My patienten.php 我的patienten.php
<?php
if($_GET["action"] == "patienten_anlegen"
{
$output = array('status' => "patienten_anlegen", 'message' => "OKAY");
}
echo json_encode($output);
?>
It doesn't work :( 它不起作用:(
But I don't understand why! 但我不明白为什么! I used the firefox debugger, but I don't see anything.
我使用了firefox调试器,但我没有看到任何东西。
you are specifying in Ajax call as type: 'post',
您在Ajax调用中指定
type: 'post',
but in PHP using as $_GET
if($_GET["action"]
但在PHP中使用
$_GET
if($_GET["action"]
Change Type to type: 'get', and I hope you have a field named action in you form. 将Type更改为type:'get',我希望你的表单中有一个名为action的字段。
POST is not via the URL query string. POST不是通过URL查询字符串。 GET is via the query string.
GET是通过查询字符串。
In your php code you should be using $_POST
instead of $_GET
. 在您的PHP代码中,您应该使用
$_POST
而不是$_GET
。
Also, the data will be posted as "name" => "value"
where name is the 'name' field of your input fields. 此外,数据将作为
"name" => "value"
,其中name是输入字段的“name”字段。
So $_POST['termin_anrede']
will contain the value of your select box. 因此
$_POST['termin_anrede']
将包含您的选择框的值。
Edit: try changing the first two lines of your patient.js to this. 编辑:尝试将您患者的前两行更改为此。
$(document).on('click', '#submit_patienten_anlegen', function(e) {
e.preventDefault();
alert("Test");
Edit 2: Here is the complete html I used to produce working results. 编辑2:这是我用来产生工作结果的完整html。 If you open your browser's console you will see console output.
如果您打开浏览器的控制台,您将看到控制台输出。
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery.js"></script>
</head>
<body>
<form id="form_patienten_anlegen" name="form_patienten_anlegen">
<table class="table_patienten_anlegen">
<tr>
<td>Anrede:</td>
<td>
<select id="termin_anrede" name="termin_anrede">
<option value=0>Bitte auswählen</option>
<option value="Frau">Frau</option>
<option value="Herr">Herr</option>
</select>
</td>
</tr>
<tr>
<td>Vorname:</td>
<td><input type="text" size="30" name="termin_vorname" id="termin_vorname" ><div class="error" id="termin_vornamefehler"></div></td>
</tr>
<tr>
<td>Nachname:</td>
<td><input type="text" size="30" name="termin_nachname" id="termin_nachname"><div class="error" id="termin_nachnamefehler"></div></td>
</tr>
</table>
<button id="submit_patienten_anlegen">Submit</button>
</form>
<script>
$(document).on('click', '#submit_patienten_anlegen', function(e) {
e.preventDefault();
alert("Test");
$.ajax({url: 'func/patienten.php',
data: $('#form_patienten_anlegen').serialize(),
parameters: {
action: 'patienten_anlegen'
},
type: 'post',
beforeSend: function() {
},
complete: function() {
},
success: function (result) {
var response = JSON.parse(result);
console.log(response);
},
error: function (request,error) {
alert('Fehler!');
}
});
});
</script>
</body>
</html>
And here is the patienten.php 这是patienten.php
<?php
echo json_encode($_POST);
?>
As you can see, the php page has received the post data into the $_POST
array where you can access the values of the form fields like so: 正如您所看到的,php页面已将post数据接收到
$_POST
数组中,您可以在其中访问表单字段的值,如下所示:
As you can see, these correspond to the name=""
attribute of each select/input field. 如您所见,这些对应于每个选择/输入字段的
name=""
属性。
I'm still not sure what you're trying to access with $_GET["action"]
but hopefully you can use/modify the code here to achieve what you need. 我仍然不确定你试图用
$_GET["action"]
访问什么,但希望你可以在这里使用/修改代码来实现你所需要的。 Comment if you need more help. 评论您是否需要更多帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.