1

I'm trying to update the action property of the form tag by calling a JavaScript function when a user presses the submit button. The action property is updated depending on what is selected in the select box called "proceed". However the following script does not work and I don't know why.

Javascript:

<script type="text/javascript"> 
   function updateAction() {
      <?php
         if (empty($errors) && isset($_POST['submit'])) {
            echo 'document.newStudent.action = document.newStudent.proceed.value';
         } else {
            echo 'document.newStudent.action = "newStudent.php"';
         }
      ?>
   }      
</script>
<form name="newStudent" method="POST" onsubmit="updateAction()">

Select box which is much further down in my code:

<?php //PROCEED
   echo '<tr>';
   echo '<td><p>Proceed To:</p></td>';
   echo '<td>
         <select name="proceed">
         <option value="newSchedule.php">add student\'s schedule</option>
         <option value="newStudent.php">add another student</option>
         <option value="staff.php">staff page</option></td>';
   echo '</tr>';
?>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1975196
  • 33
  • 1
  • 5

1 Answers1

3

You can not write PHP code inside a JavaScript method like that. You have to understand difference between server-side scripting and client-side scripting. In your scenario write a JS code which accomplish your task. Read this Client side vs server side basics, PHP vs JavaScript For Dynamic HTML Pages

Javascript and PHP allow website developers to create dynamic Web pages. Javascript is a client-side language, so it runs on the reader's computer. PHP is a server-side language, so it runs on the Web server. These two languages are combined to create interactive website's for readers. Each of these languages are combined, but they interact with the Web browser differently.

There are situation you can write PHP inside a Javascript function but not in your scenario.

Read More

Community
  • 1
  • 1
Techie
  • 44,706
  • 42
  • 157
  • 243
  • You CAN write php within javascript, just not the way the post author wrote his code. If that is what you mean by "There are situation you can write PHP inside a Javascript function." then please update, so that sentence makes sense. – Scott Sword Jan 14 '13 at 02:56
  • Is it possible to change the action using just PHP? I am unsure how I would use javascript to accomplish this as the conditions rely on PHP POST variables which are already set up. I do already understand the basics of client and server side scripting. However I don't understand why the PHP inside the javascript won't work. Won't the server execute the PHP code and send the javascript/html to the client to then render? – user1975196 Jan 14 '13 at 03:11
  • In what circumstances can I use PHP inside javascript – user1975196 Jan 14 '13 at 03:12
  • Read this http://stackoverflow.com/questions/3345457/how-to-put-php-inside-javascript – Techie Jan 14 '13 at 03:15
  • 1
    @user1975196 For instance if you were using jQuery's $.ajax call to grab some data. But lets say you don't just have one URL, you have multiple, in fact you have multiple URLs that will be coming from a database so this portion of your js needs to be "dynamically data-driven". So in using the ajax fn one of the params is url (this is where you tell the function where to go to get the data). Instead of saying url: 'http://mysite.com/data', you could say url: 'http://mysite.com/=$dataVariable?>' Now we are writing situationally correct php within our javascript. – Scott Sword Jan 14 '13 at 03:39