[英]Text area form field not passing value to JavaScript
I have a form on my website which passes its values to some JavaScript which then passes the information to a PHP page to update the database. 我的网站上有一个表单,该表单将其值传递给一些JavaScript,然后将这些信息传递给PHP页面以更新数据库。 When the form is submitted all of the values are added to the database except the two fields which are text areas.
提交表单后,除两个字段即文本区域外,所有值都将添加到数据库中。
If I remove the JavaScript and add a simple form action to directly pass the variables to the PHP page then all of the information is added to the database including the two text area fields and so it seems the problem must be in theform and the JavaScript, otherwise the two fields wouldn't get added to the database regardless. 如果我删除JavaScript并添加一个简单的表单操作以将变量直接传递到PHP页面,则所有信息都将添加到数据库中,包括两个文本区域字段,因此看来问题一定出在表单和JavaScript上,否则,这两个字段将不会添加到数据库中。
Below is my form and my JavaScript. 以下是我的表格和我的JavaScript。 ( Top keep it simple I have only included the two elements of the form which have the problems as it is a long form, if this is the wrong approach then please let me know.)
(简单起见,我只包含了表单中的两个元素,因为这是一个长格式,如果这是错误的方法,请告诉我。)
<form action="" method="post" class="save">
<label class="col-sm-3 control-label">Notes</label>
<textarea name="notes" rows="10" class="form-control"></textarea>
<input type="submit" name="submit" class="save" value="Save This Job" id="blue"/>
</form>
And now the JavaScript 现在是JavaScript
$(document).ready(function(){
$('form.save').submit(function () {
var ref_number = $(this).find("[name='ref_number']").val();
var job_title = $(this).find("[name='job_title']").val();
var start_date = $(this).find("[name='start_date']").val();
var closing_date = $(this).find("[name='closing_date']").val();
var category = $(this).find("[name='category']").val();
var location = $(this).find("[name='location']").val();
var salary = $(this).find("[name='salary']").val();
var client = $(this).find("[name='client']").val();
var job_description = $(this).find("[name='job_description']").val();
var license = $(this).find("[name='license']").val();
var notes = $(this).find("[name='notes']").val();
// ...
$.ajax({
type: "POST",
url: "save.php",
data: {
ref_number : ref_number,
job_title : job_title,
start_date : start_date,
closing_date : closing_date,
category : category,
location : location,
salary : salary,
client : client,
job_description : job_description,
license : license,
notes : notes,
},
success: function(){
new PNotify({
title: 'Job Posted',
text: '
type: 'success',
shadow: true
});
}
});
this.reset();
return false;
});
});
So here is a simplified version of the PHP which I keep changing, as I said above if I send the variables direct to the PHP page all of the information is added to the mysql database, no problems, but when using the JavaScript the job description and notes fields do not get added. 因此,这是我不断更改的PHP的简化版本,如上所述,如果我将变量直接发送到PHP页面,则所有信息都将添加到mysql数据库中,没有问题,但是当使用JavaScript时,作业说明和注释字段不会添加。
$ref_number = $_POST['ref_number'];
$job_title = $_POST['job_title'];
$start_date = $_POST['start_date'];
$closing_date = $_POST['closing_date'];
$category = $_POST['category'];
$location = $_POST['location'];
$salary = $_POST['salary'];
$client = $_POST['client'];
$job_description = $_POST['job_description'];
$license = $_POST['license'];
$posted_by = $_POST['posted_by'];
$site_id = $_POST['site_id'];
$notes = $_POST['notes'];
mysql_query("INSERT INTO joborders
(ref_number, job_title, start_date, closing_date, category, location, salary, client_name, job_description, license_required, site_id, posted_by, status, notes, posted_date) VALUES('$ref_number', '$job_title', '$start_date', '$closing_date', '$category', '$location', '$salary', '$client', '$job_description', '$license', '$site_id', '$posted_by', 'open', '$notes', NOW()) ")
or die(mysql_error());
So i found the problem, at the bottom of the page was this piece of code, which i hadn't noticed before even though i have looked at it a dozen times. 因此,我发现了问题所在,在页面底部是这段代码,即使我看了十几遍,也没有发现过。 As soon as i removed it, it worked, so now i just need to work out if i can incorporate it on to the existing code so i can still use the tnymce
我一删除它,它就起作用了,所以现在我只需要弄清楚是否可以将其合并到现有代码中,这样我仍然可以使用tnymce
tinymce.init({
selector: "textarea"
});
OK so the problem turned out to be because there was this piece of code on the page 好的,问题出在这是因为页面上有这段代码
tinymce.init({
selector: "textarea" }); 选择器:“ textarea”});
So i changed this to reference the class of the text are in the form, like this. 所以我将其更改为以此类形式引用文本的类。
tinymce.init({
selector: "textarea.form-control",
}); });
And now all of the form fields post to the database. 现在,所有表单字段都将发布到数据库中。
Thanks for all your suggestions and comments, much appreciated. 感谢您的所有建议和意见,非常感谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.