Form submit to run javascript and then post to php script

后端 未结 4 1951
暗喜
暗喜 2021-01-13 11:13

I am currently working on a php/html/javascript project. I have a form where when the user presses the submit button, it should run a javascript function and then afterwards

相关标签:
4条回答
  • 2021-01-13 11:54

    Set yout 'action' parameter to your PHP script, and do any javascript procesing in a javascript event.

    <form class="form" id="addImageForm" name="addImageForm" action="processing.php" method="post" onsubmit="return validateAddImage();">
    

    Then in your js validation, return 'true' if everything's fine, or 'false' if not. Returning 'true' means 'continue with the submit process and send over data to processing.php

    0 讨论(0)
  • 2021-01-13 11:58

    Change the action attribute to the address of the php script you're posting to. Call the javascript function in an onsubmit attribute instead of action. But you'll have to prefix it with a return statement.

    <form class="form" method="post" action="your/script.php" id="addImageForm" name="addImageForm" onsubmit=return javascript:validateAddImage()>
    

    That way if validateAddImage() returns false, the form won't be submitted. But if it returns true it will.

    0 讨论(0)
  • 2021-01-13 12:07

    Replace the action attribute with the name of your PHP page, and have the validateAddImage method as an onsubmit event.

    0 讨论(0)
  • 2021-01-13 12:18

    What you need is onsubmit.

    <form class="form" id="addImageForm" name="addImageForm" action="addImage.php" onsubmit="return validateAddImage();" method="post">
    
    0 讨论(0)
提交回复
热议问题