简体   繁体   中英

passing a variable form id from php to javascript

I'm trying to pass a dynamically created id from a form to JavaScript.

<script type="text/javascript">
$(function() {
  $(".button-call").click(function() {
    var fld = "<?= $div_id;?>";
    var test = $(fld).val();
        alert(test);
...

<form method="post" action="" enctype="multipart/form-data">
<input type="text" class="tb" name="<?php echo $div_id; ?>" id="<?php echo $div_id; ?>" value="" maxlength="80" />
<input type="submit" value="Send" class="button-call" name="p_k2" id="p_k2" />
</form>

alert(test) shows: undefined.

when I change it to alert(fld) I get two alerts, first it shows an empty box, then the correct $div_id

what am I doing wrong??

EDIT 1: I'm trying to display the text from the input...

EDIT 2: this is the complete JavaScript:

<script type="text/javascript">
$(function() {
  $(".button-call").click(function() {
    //var fld = "<?= $div_id;?>";
    var fld = "input[name=<?=$div_id;?>]";
    //var fld = "#<?=$div_id;?>";
    var test = $(fld).val();
    var dataString = 'content='+ test;

    alert(fld);
    alert(test);

    if(test=='')
    {
        alert("Write some text ...");
    }
    else
    {
        $("#flash").show();
        $("#flash").fadeIn(1000).html('<img src="images/ajax-loader.gif" align="absmiddle"> <span class="loading"> Loading ...</span>');

      $.ajax({
      type: "POST",
      url: "p_k2.php",
      data: dataString,
      cache: false,
      success: function(html){
      $("#divToRefresh").after(html);
      document.getElementById(fld).value='';
      document.getElementById(fld).focus();
      $("#flash").fadeOut(1000);
      }
      });
    }
    return false;
  });
});
</script>

EDIT 3: this is the example I'm using: http://www.9lessons.info/2009/05/insert-and-load-record-using-jquery-and.html everything works OK if I use fixed id. But can't get it working with dynamically created id.

EDIT 4: OK, there are many answers, but non of them seem to be working. I'll try to simplify.

I'll get back to this example, the one I started with (http://www.9lessons.info/2009/05/insert-and-load-record-using-jquery-and.html).

My website is something similar to Facebook. You have friends, and on your 'wall' page you have many different comment boxes (for each of your friends). To make ID's of those comment boxes different from one another (to avoid posting something to all your friends) I had to use friend id's from database ( input type="text" class="tb" name="<?php echo $div_id; ?>" id="<?php echo $div_id; ?>" value="" ). That part works OK.

I can get that id to the JavaScript ( var fld = "input[name=<?=$div_id;?>]"; ) but, I can't read the value of that text field. Once I do that, I'll take care of inserting that to my database...

var test = $("#"+fld).val();
alert(test);

try

var fld = "input[name=<?=$div_id;?>]";

or better

var fld = "#<?=$div_id;?>";

you can use $(this) inside the click event handler.

ie

$(".elementClass").click(function(){
    alert($(this).attr("id"));
});

will return the id of the .elementClass that was clicked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM