2

Please help, I am trying to get the value of input field after ajax success, I don't know why is it always undefined?

ajax.js

$(document).on('click','.modify',function(){
var modId = $(this).attr('id');
var event_id = $(this).attr('class').split(' ')[1];
$.ajax({
    cache: false,
    url: '../ajax/paraphernalia/ajax_get_edit.php',
    type: 'post',
    data: { modId: modId, event_id: event_id},
    success:function(data){
        var mod_name = $(data).find('input#hidden_headerName').val();
        alert(mod_name);
        $('#display_modal_edit').html(data);
        $('#judge_name_header').html(mod_name);
    }
});

});

ajax_get_edit.php

session_start();
require ("../../global.php");

if (isset($_POST['modId']) && isset($_POST['event_id'])) {
    $modId = $_POST['modId'];
    $event_id = $_POST['event_id'];
    $output = '';
    $sql = mysql_query("SELECT * FROM tbl_judges WHERE judge_id = ".$modId." AND event_id = ".$event_id."");
    $soc_sql = mysql_fetch_assoc($sql);

    $output .= '<input type="text" value="GetThisvalue" id="hidden_headerName">';
    $output .= '.....';//bunch of codes here        
    $output .= '</div>';

    echo $output;
}
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
ßiansor Å. Ålmerol
  • 2,849
  • 3
  • 22
  • 24
  • 1
    I am thinking the issue here is that `data` is a string and not a DOM Element. So using jQuery like that will of couse not work, you need to process a string as a string, or insert it into the DOM in some invisible div and then get the `.val()` and then delete it – RiggsFolly Aug 17 '16 at 14:46
  • Possible duplicate of [Parsing returned HTML from jQuery AJAX request](http://stackoverflow.com/questions/20007721/parsing-returned-html-from-jquery-ajax-request) – Adam Azad Aug 17 '16 at 14:47
  • 3
    Why does ajax_get_edit.php return all that HTML, if you only need the value? – Bolli Aug 17 '16 at 14:48
  • `$()` will probably parse it as a DOM element, but then you try to find `input#hidden_headerName` *within* that element – Bert Aug 17 '16 at 14:48
  • 1
    set `dataType` to `html` ($.ajax settings), and try using `.filter()` instead of `find()` – Adam Azad Aug 17 '16 at 14:49
  • @AdamAzad wow, I got it, I changed it to filter and it work thanks – ßiansor Å. Ålmerol Aug 17 '16 at 14:51
  • 2
    I am happy I could help, but consider @Bolli's suggestion. – Adam Azad Aug 17 '16 at 14:53
  • 1
    I'd rather use `method: "post"` instead of `type: "post"` as type is confusing – Evochrome Aug 17 '16 at 15:01

1 Answers1

0

You can Return a JSON from PHP and Create the Divs you need/dont Need on the Client, further its also simpler if you just Need some values

webdeb
  • 12,993
  • 5
  • 28
  • 44