[英]Insert data from multiple text boxes into sql via ajax
Not sure of the best way do this. 不确定最好的方法是这样做。
On my page I have the following (which pulls a list of custom questions from my database set by a user) 在我的页面上,我有以下内容(从用户设置的数据库中拉出一系列自定义问题)
PHP PAGE THAT SHOWS THE QUESTIONS / COVER LETTER TEXT AREA 显示问题/封面字母文本区域的PHP页面
$questions = $this->db->get_where("applicationquestions",
["opportunity_id" => $contact->id]);
$questionsfound = $questions->num_rows();
$questions = $questions->result();
<? foreach ($question as $q):?>
<div class="form-group">
<label for="<?echo $q->id;?>" class="control-label"><? echo $q->label;?
>:</label>
<input type="text" class="form-control" id="<?echo $q->id;?>" name="<?echo $q->id;?>">
</div>
<?endforeach;?>
and using ajax / javascript i am passing information via POST 并使用ajax / javascript,我通过POST传递信息
** THE JS ** $("#apply").click(function(e) { e.preventDefault(); ** JS ** $(“#apply”)。click(function(e){e.preventDefault();
var oppid = "<?echo $opportunity->id;?>";
$.ajax({
url:
"https://MYSITEHERE/submitapplication",
method: "POST",
data: {oppid:oppid}}); });
What i am wondering is the best way to get then insert the questions id and the users answer into my database through this method. 我想知道的是最好的方法,然后通过这种方法将问题ID和用户答案插入我的数据库。
** Submit Application File / Function ** **提交申请文件/功能**
public function submitapplication() {
$insert['opportunity_id']= $this->input->post('oppid');
$insert['user_id']= is_user_logged_in();
$insert['time']= time();
$insert['coverletter']= $this->input->post('coverletter');
$this->db->insert("applications", $insert);
// here i would need it to submit the answers from the question textboxes into the table applicationanswers along with the question id //在这里,我需要它将问题文本框中的答案与问题ID一起提交到表格applicationanswers中
}
HTML THAT IS DISPLAYED AFTER PHP HAS LISTED QUESTION FIELDS 在PHP之后显示的HTML列出了问题字段
<form id="applyform" class="">
<div class="row">
<div class="col-md-6 col-xs-12">
<label>Cover Letter</label>
<textarea class="form-control" id="coverletter" name="coverletter" rows="7" placeholder="Start typing a cover letter"></textarea>
<div class="form-group">
<label for="1" class="control-label">Have you sold web design before?:
</label>
<input type="text" class="form-control" id="1">
</div>
<div class="form-group">
<label for="2" class="control-label">Do you like monkeys?:</label>
<input type="text" class="form-control" id="2">
</div>
</div> </div>
</form>
If you are using more than one item in your form, I would suggest using 如果您在表单中使用多个项目,建议您使用
var data = $('form').serialize();
This will pass all of the form values to your php script and then you can use php to insert them. 这会将所有表单值传递给您的php脚本,然后您可以使用php插入它们。
$("#apply").click(function(e) {
e.preventDefault();
var oppid = "<?echo $opportunity->id;?>";
var data = $('form').serialize();
$.ajax({
url:
"https://MYSITEHERE/submitapplication",
method: "POST",
data: {formdata:data}}); });
You would then just parse the data in your php 然后,您只需解析PHP中的数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.