0

Hi I want to validate a textfield with javascript. Here is my code in php:

$question_type = "<input type='text' id='question_type_n' onkeyup="allnumeric(this.value)" name='question_type_n_".$qid."' value='".$d[0]."' >";

I also have the function allnumeric() in javascript. But it returns errors. Javascript-Error:

Parse error: syntax error, unexpected 'allnumeric' (T_STRING) in E:\Share\xampp\htdocs\Dropbox\icsurvey\application\views\finish_survey.php on line 142
1
  • I think your problem is with nested quotes. Try single quotes instead.. $question_type = "<input type='text' id='question_type_n' onkeyup='allnumeric(this.value)' name='question_type_n_".$qid."' value='".$d[0]."' >"; Commented Sep 26, 2014 at 5:20

4 Answers 4

2

You have to escape it properly

$question_type = "<input type='text' id='question_type_n' onkeyup='allnumeric(this.value)' name='question_type_n_".$qid."' value='".$d[0]."' >";

First thing is that you dint concatenate the string properly. Second error being php wont understand the js function allnumeric. You have to embed it as string so that it will be triggered on the onclick event on your browser not at the serverside.

Sign up to request clarification or add additional context in comments.

Comments

0

instead of using onkeyup="allnumeric(this.value)" use in single quotes like onkeyup='allnumeric(this.value)'

4 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
@user503413 This does provide a solution. Simple solutions are still answers.
@SpencerWieczorek - thank you for your feedback, you could have used the time to edit the question to add some simple explanation why the provided code snippet would provide a solution and what was wrong in what the poster tried. And also, if you consider it that good as a response next to the question there is a up-vote button you can express your opinion in that way.
@user503413 I'm simply stating that this is a valid answer, such that it provides a solution like the other answers. Not that it is a good answer, simply that it's not violating the rules. I don't think this is a good response, because the lack of explanation. And there is no need to edit because there are already better responses to this question.
0

PHP outputs HTML, in which can contain JavaScript. Note you need to use . to concatenate in PHP. But in this case we just need to use single quotes:

$question_type = "<input ... onkeyup='allnumeric(this.value)' ... >";

Comments

0

Try this on your PHP code:

$question_type = "<input type='text' id='question_type_n' onkeyup='allnumeric(this.value)' name='question_type_n_".$qid."' value='".$d[0]."' >";

Comments

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.