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

Link to comment
Share on other sites

 

 

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

Link to comment
Share on other sites

  • 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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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