1
<?php   
 echo "*Select Category<select name='category' id='category' onChange='ajax('fill.php', '', 'populate', 'post', '1')'><option value=''>--Please Select--</option><option value='1' >1</option><option value='2'>2</option></select>";
?>

What should come instead of codes(') for the arguements inside ajax function.

Gaurav Gandhi
  • 161
  • 2
  • 2
  • 9

3 Answers3

2

Use backslashed quotes

onChange='ajax(\"fill.php\", \"\", \"populate\", \"post\", \"1\")'

HTML attributes should be in quotes, swap ' and " in the code instead

echo '*Select Category<select name="category" id="category" onChange="ajax(\'fill.php\', \'\', \'populate\', \'post\', \'1\')"><option value="">--Please Select--</option><option value="1" >1</option><option value="2">2</option></select>';

To increase readability, use HEREDOC syntax

echo <<<STR
  *Select Category
  <select name="category" id="category" onChange="ajax('fill.php', '', 'populate', 'post', '1')">
    <option value="">--Please Select--</option>
    <option value="1">1</option>
    <option value="2">2</option>
  </select>
STR;
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
1

this will work:

onChange=\"ajax('fill.php', '', 'populate', 'post', '1')\"

but it is even better and cleaner to assign the change event via jquery

$('#category').change(function(){
    ajax('fill.php', '', 'populate', 'post', '1');
});

if you dont want to use jquery you should put your javascript in a seperate function to have just a call inside your onchange:

onChange='callAjax()'

[...]

function callAjax() {
    ajax('fill.php', '', 'populate', 'post', '1');
}

Please read this thread in context When should I use Inline vs. External Javascript?

Community
  • 1
  • 1
steven
  • 4,868
  • 2
  • 28
  • 58
  • 1
    He has not marked the question as using jQuery or whatever other framework is using $, so the second option might not work for him. – Precastic Jun 25 '13 at 09:33
  • it was just an advice. You can avoid much pain and work by using it. – steven Jun 25 '13 at 09:36
1

Simply alternate your inverted commas between double & single & escape where needed like so:

echo "*Select Category <select name='category' id='category' onChange=\"ajax('fill.php', '', 'populate', 'post', '1')\"><option value=''>--Please Select--</option><option value='1' >1</option><option value='2'>2</option></select>";

Specifically, changing:

onChange='...'

to

onChange=\"...\"
Precastic
  • 3,742
  • 1
  • 24
  • 29