PHP input form javascript integration failed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bgbs
Forum Newbie
Posts: 6
Joined: Thu Mar 12, 2009 11:23 am

PHP input form javascript integration failed

Post by bgbs »

I have this input box wrapped in php code. (I've only pasted the portion of that php code.)

Code: Select all

<?php
case 'email':
            $html .= '
    <input  name="s" id="s" value="Email..." type="text" size="18" value="'.esc_html($var['default']).'" name="'.esc_attr($opt).'" id="'.esc_attr($opt).'" class="mc_input"/>';
    break;
?>
And I would like to add this javascript into the input form

Code: Select all

onfocus="if (this.value == 'Search...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search...';}
I tried this and got a syntax error

Code: Select all

<input  name="s" id="s"  onfocus="'.if (this.value == 'Search...') {this.value = '';}.'" onblur="'.if (this.value == '') {this.value = 'Search...';}.'" value="Email..." type="text" size="18" value="'.esc_html($var['default']).'" name="'.esc_attr($opt).'" id="'.esc_attr($opt).'" class="mc_input"/>';
Someone suggested to me that I should be using $html plus slashes to accomplish this, but I dont think thats right, because the onfocus and onblur should be inside the input tags.

Any help appreciated
Thanks in advance
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP input form javascript integration failed

Post by Celauran »

The problem is that you've got a number of mismatched quotes.

Code: Select all

$html = '<input  name="s" id="s"  onfocus="if (this.value == \'Search...\') {this.value = \'\';}" onblur="if (this.value == \'\') {this.value = \'Search...\';}" value="Email..." type="text" size="18" value="'.esc_html($var['default']).'" name="'.esc_attr($opt).'" id="'.esc_attr($opt).'" class="mc_input"/>';
Post Reply