Jump to content

Writing a php variable in js ?


Recommended Posts

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

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.

 

 

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.

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.