1

I have a form created using echo command in php. It is being displayed correctly. I have a javascript which detects click on radio button of form and displays the hyperlink/anchor corresponding to each button group, which is not working.

But if I create the form in html only without any php, it works perfectly. please HELP.

Form creation

echo("<form name ='input' action = 'result.php' method = 'POST'>");
while($row = mysqli_fetch_array($result,MYSQLI_NUM))
{
    echo(($i+1)." ".$row[1]."<br>");
    echo("
    <input type = 'radio' value = $row[2] name = '$i'>$row[2]&nbsp&nbsp&nbsp&nbsp
    <input type = 'radio' value = $row[3] name = '$i'>$row[3]<br>
    <input type = 'radio' value = $row[4] name = '$i'>$row[4]&nbsp&nbsp&nbsp&nbsp
    <input type = 'radio' value = $row[5] name = '$i'>$row[5]&nbsp&nbsp
    <a id='$i' href='javascript:clear($i)'>Reset</a><br>");
    $i++;
    $z[]=$row[6];
}

SCRIPT

$('input:radio').click(function() { 
    var n = $(this).attr('name');
    var k ='#' + n;
    alert($(this).attr('name')); 
    $(k).show(400);
    });
11
  • what error are you getting in consol? Commented Oct 4, 2013 at 15:35
  • And the generated HTML sent to the browser would also be helpful. Commented Oct 4, 2013 at 15:37
  • try input[type=radio] Commented Oct 4, 2013 at 15:37
  • Try putting the html output. php's echo isn't your problem because that is interpreted in the server. In this code I can say. 1) Close the form. 2) quote the radio value. Those aren't errors per se but will show cleaner output Commented Oct 4, 2013 at 15:38
  • @ErikNedwidek this is the generated HTML sent to browser : pastebin.com/eNviPC09 Commented Oct 4, 2013 at 15:41

3 Answers 3

2

You can try changing $('input:radio') to $('input[type="radio"]').

Try this

$('input[type="radio"]').on('click', function() { 
    var n = $(this).attr('name');
    var k ='#' + n;
    alert($(this).attr('name')); 
    $(k).show(400);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Could you expand your answer a bit more about the difference between those two selectors?
The difference in those selectors is that the second uses the [attribute=value]. The : is used with pseudo classes and radio is not a pseudo class. Examples of pseudo classes are input:first-child, input:focus, etc.
0
$('body').on('click','input:radio',(function() { 
  var n = $(this).attr('name');
  var k ='#' + n;
  alert($(this).attr('name')); 
  $(k).show(400);
});

When clicking on dynamically created elements you need to use event delegation.

5 Comments

Form is generated server-side, there are no AJAX calls in the question, why do you think it is dynamically created?
Not if it was generated using php, AJAX maybe but not php alone.
Only because he said that his same jquery was working in pure html. But if I create the form in html only without any php so I assumed that maybe the php was being shown after the document was initially loaded.
That would lead me to believe that something is wrong with his html generation via php and not his java script then.
Great, If your html body is really big i.e. contains more then just your form I would recommend surrounding your form by a div and doing $('#yourDivID').on('click','input:radio'.. etc.. so that its just listening on your div for clicks and not the whole body. It would improve performance on large html files. Note: the div would need to be part of your original html and not generated by your php.
0

Hmm... your problem is that $row array should be with curly brackets because you're in double quotes

echo("<form name ='input' action = 'result.php' method = 'POST'>");
while($row = mysqli_fetch_array($result,MYSQLI_NUM))
{
    echo(($i+1)." ".$row[1]."<br>");
    echo("
    <input type = 'radio' value = '{$row[2]}' name = '$i'>{$row[2]}&nbsp&nbsp&nbsp&nbsp
    <input type = 'radio' value = '{$row[3]}' name = '$i'>{$row[3]}<br>
    <input type = 'radio' value = '{$row[4]}' name = '$i'>{$row[4]}&nbsp&nbsp&nbsp&nbsp
    <input type = 'radio' value = '{$row[5]}' name = '$i'>{$row[5]}&nbsp&nbsp
    <a id='$i' href='javascript:clear($i)'>Reset</a><br>");
    $i++;
    $z[]=$row[6];
}

1 Comment

This would have flagged Encaps error, OP didn't show any error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.