is it possible to have php code within jquery or do i have to use ajax to grab the values.

ex of what i was trying

function blah() {
	//Creates the name tag	
	$("#newitem").append("<tr><td><fieldset><legend><select name='produce'><option>--Select--</option><?=produce();?> </select></legend><table width='200'><tr><td>Ammount</td><td>On Hand</td><td>Cost</td></tr><tr id='txtHint'></tr>");

as you can see that i have a php function called produce(). but when i try to use this it doesnt work. if i take out the php code it works.
hope that there is an easy fix for this

thanks in advanced

7 Years
Discussion Span
Last Post by itisnot_me

If you have that code on a php page then it will try to call the function and insert the output directly into that string. You have to understand the difference between a server-side language and a client-side language. PHP will get executed before the javascript even hits the browser.

What you code in somepage.php

// this stuff never touches the browser
<script type="text/javascript">
alert("<?php echo 'Hello World!'; ?>");

When the page gets to the user there is no longer any PHP code in the page, the server has already interpreted it and given the results so the browser receives

<script type="text/javascript">
alert("Hello World!");

Edited by ShawnCplus: n/a


thanks for the help.

i understand the way server vs browser works. but you helped me realize that i made a stupid mistake. i did not make my php function echo the proper syntax so the jquery never executed.

thanks for making me use my brain. this is what happens when you start out with a new area of coding.


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.