Jump to content

Help! Integrating Javascarpt in PHP form


bgbs

Recommended Posts

Hi

I have this input box wrapped in php code

 

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

onfocus="if (this.value == 'Search...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search...';}

 

So, the newb that I am, I tried this and got a syntax error

<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"/>';

 

Any ideas how to properly integrate this javascript code into PHP?

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/255395-help-integrating-javascarpt-in-php-form/
Share on other sites

I would actually assign both tasks to their own js functions, and call them via the event caller in your markup.

however, to answer your question.

 

<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(<?php echo $var['default']; ?>)" name="esc_attr(<?php echo $opt; ?>)" id="esc_attr(<?php echo $opt; ?>)" class="mc_input"/>';

 

 

This way will not work because I'm already working within php. Another words the statement below is already contained in php brackets, so echoing php within php will not work.

<?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;
?>

  • 2 weeks later...

Hello,

 

It is possible what you want to do, Just check code i have modified for you as follows:

 

$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(<?php echo $var['default']; ?>)" name="esc_attr(<?php echo $opt; ?>)" id="esc_attr(<?php echo $opt; ?>)" class="mc_input"/>';

 

 

I have escape  single quote in your string assignment as you have put whole string in '' code.

 

Hope you can understand what i mean

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.