开发者

get textbox id dynamically jquery php

http://jsf开发者_C百科iddle.net/boyee007/kS6Vr/

how do i retrieve the dynamic textboxes id using jquery ajax pass it to PHP

JQUERY AJAX:

$("#book_event").submit(function(e) {
    $(this).find('input[id^=textbox]').each(function(i, e) {
        $.post("Scripts/book_event.php", {  att_name: $(e).val() }, function(data) {
            if (data.success) {
                $("#err").text(data.message).addClass("ok").fadeIn("slow");
            } else {
                $("#err").text(data.message).addClass("error").fadeIn("slow");
            }
        }, "json");
    });
    e.preventDefault();
});

and how do i get those id with PHP:

if(!$_POST['submit']) :
   $att_name = trim($_POST['att_name']);
endif;


i would collect all the textbox inputs into a javascript array and then send it to the php script. That way you have just one ajax call instead of n.

var input_array = {events:[]};
$("#book_event").submit(function(e)
{

    $(this).find('input[id^=textbox]').each(function(i, e)
    {
        input_array.events.push(e.val());
    });


    //send array via ajax
    $.ajax
    ({
      url:  "Scripts/book_event.php",
      type:"POST",
      data:JSON.stringify(input_array),
      contentType:  'application/json; charset=utf-8',
      dataType:"json",
      success:function(data){//func on success},
      error:function(data){//func on error}
    });

    e.preventDefault();
});

on the server side now you have to retrive the json object which has been sent:

$dto = json_decode($GLOBALS["HTTP_RAW_POST_DATA"]);

foreach($dto->events AS $event)
{
   //do your work here
}
//output response

i think there should be security issues reading directly HTTP_RAW_POST_DATA, it is better to check it before decode to json

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜