How can JavaScript functions work in php loop?
Get the solution ↓↓↓How can javascript work in php loop? below is my sample codes, I need to click button and then take the value from name textbox based on the loop button I click.
<?php
for($count=0;$count<=4;$count++)
{
echo"
<form action=\"\" method=\"POST\" enctype=\"multipart/form-data\" onSubmit=\"return false;\">
<input id=\"name\" type=\"text\" name=\"name\">
<input type=\"button\" name=\"send\" id=\"but_upload\" value=\">\">
";
?>
<script>
$(document).ready(function(){
$("#but_upload").click(function(){
var fd = new FormData();
fd.append('name', document.getElementById('name').value);
if ($('#name').val()) {
$('#name').val('');
}
});
});
</script>
<?php
}
?>
Please anyone can help me
Answer
Solution:
I think you want to do something like this:
<?php
for($count=0;$count<=4;$count++)
{
echo"
<form action=\"\" method=\"POST\" enctype=\"multipart/form-data\" onSubmit=\"return false;\">
<input class=\"name\" type=\"text\" name=\"name\">
<input type=\"button\" name=\"send\" class=\"but_upload\" value=\">\"></form>
";
}
?>
<script>
$(document).ready(function(){
$(".but_upload").click(function(event){
var fd = new FormData();
var nameEl = event.target.form.querySelector('.name')
fd.append('name', nameEl.value);
if (nameEl.value) {
nameEl.value = '';
}
});
});
</script>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: undefined array key
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.