Jump to content

Unable to pull a php input array into JQuery


ajoo

Recommended Posts

Hi all, 

This bit of code creates an array of input fields using php:-

.
.
.
$count = 10;
echo "<tr>";
  for($j=0; $j<$count; $j++)
  {
     echo "<td class=userinp> <input type='text' id='reply[]' value='' ></td>";
  }	
echo "</tr>";
.
.                       
                    

I wish to process the values entered in the input fields using JQuery. How can I fetch these user inputs into JQuery correctly ?

My JQuery code is :

		$("#checkbutton").click(function(){
			var test_arr = $("input#reply");
			$.each(test_arr, function(i, item) {
				val = $(item).val();
				alert(i+" : "+ val);
			});
		});

which displays only the first value of the user input box. It is thus not fetching the array . Please point out the mistake or the correct method to achieve this.

Thanks !

 

 

 

Link to comment
Share on other sites

ok Guys, I got it !

While this

var test_arr = $("input#reply");

works for dynamically created input boxes using JS, it does not work for boxes created using php.

This works though

test_arr = $("input[id='reply[]']");

 I thought the first one looked more elegant !

Thanks !

 

Link to comment
Share on other sites

First off, for form fields you use the name attribute to define the name they are submitted as, not the id attribute.  Secondly you can't have multiple elements with the same id attribute in html. ID's have to be unique. 

Once you fix that, then you can either query for them by the name or by giving them all a common class and using a class selector.

<input type='text' name='reply[]' value=''>
var test_arr = $('[name="reply[]"]');

 

Edited by kicken
  • Great Answer 1
Link to comment
Share on other sites

Hi Kicken !

Thanks for the reply and for introducing me to the correct usage ! Even though  I am getting the correct answer by using an array for both names and id's and then targeting the id to get the values of the array in JS, getting to know the correct form is so essential. 

Thanks loads for the correction !

 

 

Link to comment
Share on other sites

Hi, 

I have another but related question, so I will continue it here rather than in a new thread. I hope that is ok.

SO if I modify my code as shown by Kicken like below:-

<?php 

$count = 10;
echo "<tr>";
  for($j=0; $j<$count; $j++)
  {
     echo "<td> <input type='text' name='reply[]'class="ansbox" value=''></td>";
  }	
echo "</tr>";
?>

and once the form has submitted the 10 post values, how can I print those back into their respective places using PHP not JS. I know it will have to echo them inside their value ' ' fields, having checked first if they have been set using isset( .. ), but I don't know the correct syntax to achieve that. 

Thanks all !

 

Edited by ajoo
Link to comment
Share on other sites

Hi Guru Barand, 

For an HTML form that is not echoed out in php, the $value variable is checked that it exists, before it is echoed out.

value="<?php if(isset($value)) echo html_escape($value);?>"

 

How Is it possible to do the same here ?

echo "<td> <input type='text' name='reply[]'class="ansbox" value='$value'></td>";

 I have tried without success. 

Thanks.

Edited by ajoo
Link to comment
Share on other sites

The values would be coming into PHP as part of the $_POST array so you'd check if they exist there and if so echo the value back out.

<?php 
$count = 10;
echo "<tr>";
for($j=0; $j<$count; $j++)
{
    $value = $_POST['reply'][$j] ?? '';
    echo "<td> <input type='text' name='reply[]' class='ansbox' value='" . htmlspecialchars($value) . "'></td>";
}	
echo "</tr>";

 

  • Great Answer 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.