trampolinejoe Posted May 1, 2009 Share Posted May 1, 2009 Hello Guys, I am making a form which loads different menu options depending on what the users previous selections are. I am also making the form so that if somebody loads the form and they have been to the page before it will pre-fill the form to there specific details. When I write with php in between the js the form stops working, why would this be? Here is what I am trying to do: function change() { switch (document.getElementById("product").value) { case "OptionA": document.getElementById("change").innerHTML = " <select name='net' id='net'> <option value='tentA'<?=$netA; =>>Include Net + $129.00</option> <option value='tentB'<?=$netB; =>>Do No Include Net</option>" break; case "OptionB": document.getElementById("change").innerHTML = " <select name='tent' id='tent'> <option value='tentA' <?=$tentA; =>>Include Net + $129.00</option> <option value='tentB' <?=$tentB; =>>Do No Include Net</option>" break; } Link to comment https://forums.phpfreaks.com/topic/156374-writing-a-php-variable-in-js/ Share on other sites More sharing options...
Mchl Posted May 1, 2009 Share Posted May 1, 2009 Try <?=$tentA; ?> Also note, that using short tags is generally discouraged. Link to comment https://forums.phpfreaks.com/topic/156374-writing-a-php-variable-in-js/#findComment-823304 Share on other sites More sharing options...
trampolinejoe Posted May 1, 2009 Author Share Posted May 1, 2009 Hey man, Why is using short tags discouraged? Also, what is the difference between what I was doing already and what your code is? Thanks man. Link to comment https://forums.phpfreaks.com/topic/156374-writing-a-php-variable-in-js/#findComment-823314 Share on other sites More sharing options...
Mchl Posted May 1, 2009 Share Posted May 1, 2009 Why is using short tags discouraged? http://www.php.net/manual/en/language.basic-syntax.phpmode.php Also, what is the difference between what I was doing already and what your code is? Mine ends with ?> instead of =>> Link to comment https://forums.phpfreaks.com/topic/156374-writing-a-php-variable-in-js/#findComment-823322 Share on other sites More sharing options...
trampolinejoe Posted May 1, 2009 Author Share Posted May 1, 2009 hey man, Thanks for that, well I had a read and it said that they are less portable, do you know what that means exactly? And also, I tried the tag your style with the ?> and it is being very strange, its actually breaking the option topdown. I am having trouble testing my could because when I right click in the browser to try and see what the output of the code is I can not see any code because the code that was generated by innerHTML arnt appearing in the source code. So I dont know what to do because the code is hidden so anything I try and test I can't see the output. What I am getting is an extra '>' appearing as a string in the name in mydrop down menu and also actually set the value of the variable as [$netA ='selected="selected"'; ]but the thing is that means that item should be selected on load if the php was actually writing in the js. It is not appearing as selected so I am alittle confused. Link to comment https://forums.phpfreaks.com/topic/156374-writing-a-php-variable-in-js/#findComment-823381 Share on other sites More sharing options...
trampolinejoe Posted May 1, 2009 Author Share Posted May 1, 2009 this fourm says it can not be done, is this true? http://www.ozzu.com/programming-forum/can-you-write-php-inside-javascript-code-t38065.html Link to comment https://forums.phpfreaks.com/topic/156374-writing-a-php-variable-in-js/#findComment-823386 Share on other sites More sharing options...
Mchl Posted May 1, 2009 Share Posted May 1, 2009 Remember that PHP runs on server, before the page is sent to browser and rendered. If you're trying to us PHP to change values while in browser, then it is not possible. "Less portable" means: there are many servers that do not support these tags. If you try to run your code on them it will fail. The real reason why they're discouraged is that they are the same as xml opening tags which can lead to confusion. Link to comment https://forums.phpfreaks.com/topic/156374-writing-a-php-variable-in-js/#findComment-823395 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.