Caner Ergez Caner Ergez - 1 year ago 66
PHP Question

jqeury ajax field is return 'undefined'

Project Created By Codeİgniter

Page and Response

Form Code

$form_option = array('id' => $project->baslik ,'class' => 'form-horizontal');

echo form_open('ProjectStudent/add_project_student', $form_option); ?>

<input id="project_id" type="text" name="project_id" value="<?php echo $project->id; ?>" disabled="disabled"/>
<div align="right" class="col-md-12">
<select id="lesson_id" name="lesson_id">
<option value="secim1_id">Seçim 1</option>
<option value="secim2_id">Seçim 2</option>
<option value="secim3_id">Seçim 3</option>
<button id="DersSec" type="button" class="btn btn-sm green">Ekle</button>

And jQuery Code

$(document).ready(function() {

var project_id = $(this).parent().find('input[name="project_id"]').val();
var lesson_id = $(this).parent().find('select[name="lesson_id"]').val();
var data = $(this).parent().serialize();

lesson_id is return a true data but project_id and data is not working

Answer Source

The project_id field is out of the button's parent, so when you do

var project_id = $(this).parent().find('input[name="project_id"]').val();

you actually say: go to <div align="right" class="col-md-12"> (the parent) and in it, look for an input field with the name project_id.

You just need to move the field inside the div, or access it with the upper parent like so:

var project_id = $(this).parents().eq(1).find('input[name="project_id"]').val();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download