开发者

How to grab a jquery-generated form field value where the id includes a dynamically generated int --using PHP

So I'm working on an object-oriented PHP website that uses the MVC model. In my view, I have the following code that is generating a form:

    //Handle add custom module button click

      $('.custom-btn').click(function() {

    var id = $('.custom-fieldset').length + 1;
    var fieldset = '<fieldset style="display: none;" id="custom_module_fieldset_'+id+'" class="custom-fieldset fancy"><legend>Title goes here</legend><label 

    id="label_custom_edit_title_'+id+'" for="custom_edit_title_'+id+'">
    <span>Edit title:</span><input id="custom_edit_title_'+id+'" type="text" name="custom_edit_title_'+id+'" value=""></label><label id="custom_label_edit_desc_'+id+'" for="custom_edit_desc_'+id+'"><span>Edit description:</span><textarea id="custom_edit_desc_'+id+'" name="custom_edit_desc_'+id+'" rows="5"></textarea></label><input id="custom_to_default_'+id+'" type="button" name="custom_to_default_'+id+'" class="custom-confirm-btn" value="Delete this Custom Module"></fieldset>';

    $('#add_custom_module').before(fieldset);
    $('#custom_module_fieldset_'+id).fadeIn();

    return false;
  });

I need to insert these values into the db, which means getting them back to the contro开发者_运维知识库ller page. I'm trying to do this:

$ctt = 'custom_edit_title'.$id; 
$ctd = 'custom_edit_desc'.$id; 
$customTextTitle = $_POST[$ctt]; 
$customTextDescription = $_POST[$ctd];

I've tried without single quotes, and messed around a bit, but ultimately I'm not sure how to handle the +id+ in the fieldnames in the jquery-generated form. How do I then set that part of it in the php variable?

TO SUMMARIZE::: How do I grab the form field in jquery and feed it back to the controller written in PHP?


I would write a short PHP script to insert the data into the database, or, if you are using MVC, write a method inside your controller.

Then use any of jquery's $.ajax methods to send the jquery generated variables to the appropriate method inside your controller, which will then write to the database (and send back any appropriate information, if you want to go that far)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜