0

So I have this simple form:

<form action="includes/process.php" method="post" name="standard_use" id="standard_use" enctype="multipart/form-data">

        <button onclick="dofunction(); return false;">Do it!</button>

        <input type="file" id="upload_file" name="filename" style="float:left;width:70%;" size="42"/>

</form>

So what happens really when the button is clicked ? Is it that the php file is called ? does it not ? the javascript is called before ? Anyone can shed some light on this ? Thanks !

0

3 Answers 3

2

Well, when you hit the button the following events occurs:

  1. You send a REQUEST to the server
  2. The php codes evaluates the request and runs some codes
  3. Finally it returns back a RESPONSE which you see as a web page

Javascript is a client-side script which means that whenever you make an action on the page, the code runs. For instance, when you click the button, before sending the request javascript will work. You may, for instance, place a function that will be triggered when you hit the button which checks the form and either approves the form or shows the error messages

EDIT

As far as your comment is concerned:

Yes, javascript runs first when you hit the submit button. Php runs only when you submit the form and make a request to the server.

Consider this example: (I am better at explaining things with examples:)

<form action="somepage.php" onsubmit="return checkMe()" method="POST">
    <input name="firstname" id="fn" value="" type="text" />
    <input type="submit" value="Submit" />
    <script type="text/javascript">
          function checkMe(){
              var tb = document.getElementById("fn")

              if(tb.value == "Alex") return true;
              else return false;
          }
    </script>
</form>

So basically, when you hit the button and try to submit the form, the javascript will first check whether the name provided in the textbox is Alex or not, if it is not then it will not submit the form. If it is Alex then it will submit the form and then the form will redirect the user to somepage.php. Finally, the php codes will work in somepage.php and the page will be rendered again.

Sign up to request clarification or add additional context in comments.

4 Comments

So the Javascript runs first ? What about the return false on the very last javascript? Is the form being submitted ? Or does the php file is running without being submitted ? Thanks
This isn't true either. Assuming dofunction is a function that executes without throwing an exception, the form won't submit when you click the button because of the return false
Thanks for the answer ! I already marked as answered, if you check mkk's answer he pointed out to me that there is a form.submit() in the function, so now I know the javascript can invoke the php. That was my missing link. But Thanks !
@Ted, you're welcome :) Indeed yes, you invoke a form.submit() and that's why I've put the onsubmit="" on the form tag :) Anyways, glad to help!
1

What happens is that only doFunction() javascript function is invoked and nothing more. However, it might be possible that this javascript function invokes "submit" event on the form and the request is sent (what you described as "php file is called").

1 Comment

That's about the exact case... :)
0

Your code just trigger javascript event and your function. To submit a form you need an

<input type="submit" value="Submit" />

or a button, which default type is submit (thx davin)

<button value="Submit" />

However as far as you return false in your javascript code your form won't be submitted even with the submit button.

3 Comments

Not true. The standard considers a type-less button to be of type submit. w3.org/wiki/HTML/Elements/button w3.org/TR/html4/interact/forms.html#h-17.5
@davin thx for pointing that out. I always use <input type=submit>, I wasn't aware of that. Updated the answer.
If we're aiming for excellence then I suggest adding the fact that the return false will stop form submission assuming the call dofunction() finishes with no errors. Although that might be nitpicking somewhat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.