Note that there are some explanatory texts on larger screens.

plurals
  1. POpassing a variable form id from php to javascript
    primarykey
    data
    text
    <p>I'm trying to pass a dynamically created id from a form to JavaScript. </p> <pre><code>&lt;script type="text/javascript"&gt; $(function() { $(".button-call").click(function() { var fld = "&lt;?= $div_id;?&gt;"; var test = $(fld).val(); alert(test); ... &lt;form method="post" action="" enctype="multipart/form-data"&gt; &lt;input type="text" class="tb" name="&lt;?php echo $div_id; ?&gt;" id="&lt;?php echo $div_id; ?&gt;" value="" maxlength="80" /&gt; &lt;input type="submit" value="Send" class="button-call" name="p_k2" id="p_k2" /&gt; &lt;/form&gt; </code></pre> <p>alert(test) shows: undefined.</p> <p>when I change it to alert(fld) I get two alerts, first it shows an empty box, then the correct $div_id</p> <p>what am I doing wrong??</p> <p>EDIT 1: I'm trying to display the text from the input...</p> <p>EDIT 2: this is the complete JavaScript:</p> <pre><code>&lt;script type="text/javascript"&gt; $(function() { $(".button-call").click(function() { //var fld = "&lt;?= $div_id;?&gt;"; var fld = "input[name=&lt;?=$div_id;?&gt;]"; //var fld = "#&lt;?=$div_id;?&gt;"; 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('&lt;img src="images/ajax-loader.gif" align="absmiddle"&gt; &lt;span class="loading"&gt; Loading ...&lt;/span&gt;'); $.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; }); }); &lt;/script&gt; </code></pre> <p>EDIT 3: this is the example I'm using: <a href="http://www.9lessons.info/2009/05/insert-and-load-record-using-jquery-and.html" rel="nofollow">http://www.9lessons.info/2009/05/insert-and-load-record-using-jquery-and.html</a> everything works OK if I use fixed id. But can't get it working with dynamically created id.</p> <p>EDIT 4: OK, there are many answers, but non of them seem to be working. I'll try to simplify. </p> <p>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). </p> <p>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 (<code>input type="text" class="tb" name="&lt;?php echo $div_id; ?&gt;" id="&lt;?php echo $div_id; ?&gt;" value=""</code>). That part works OK.</p> <p>I can get that id to the JavaScript (<code>var fld = "input[name=&lt;?=$div_id;?&gt;]";</code>) 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...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload