Skip to content Skip to sidebar Skip to footer

Combing JS And PHP On One Button. Is It Possible?

Hiya: i know some people would be so tired of my questions, but I'm working on a uni project and need to get it done as soon as possible. This question is about using JS on a butto

Solution 1:

This can be a way

Submit the form through JS after removing parameter

<script type="text/javascript">
    function formAction(){
        var x=document.getElementById("collect")
        x.remove(x.selectedIndex);
        document.forms[0].submit();
    }

    </script>

Input type button

<input type="button" onclick="formAction()" name="Collect"  value="Collect" />

Solution 2:

Embed jQuery and use $.post() to send an AJAX request.


Solution 3:

JavaScript can interact with the button whilst the user is navigating the page and entering data into the form. The instant the user pushes the submit button and the request for the form submission is sent JS no longer has control. The request is sent to the form's action (most likely a PHP file) which processes the request and gives an answer back.
If you really need to combine the two, look into AJAX.


Solution 4:

<?php print_r($_POST); ?>
<script type="text/javascript">
function formAction(){
    var x=document.getElementById("collect");
    x.remove(x.selectedIndex);
    submit_form();
}
function submit_form() {
    document.form1.submit();
}
</script>
<form method="post" name='form1'>
    <input type='hidden' name='Collect'/>
    <select id="collect" name="Select1" style="width: 193px">
    <option>guns</option>
    <option>knife</option>
    </select> <input type="button" onclick="formAction()" name="Collect"  value="Collect" /></form>
<? 
    if (isset($_POST['Collect'])) {
    //do whatever update you want
    }

?>

Solution 5:

Simple Solution

Make this modification in the form tag

<form method="post" onsubmit="return formAction()">

In JavaScript function add a line "return true;" at the end of the function.

Voila ..!!! you are done..!!

Enjoy..!!


Post a Comment for "Combing JS And PHP On One Button. Is It Possible?"