Home > front end >  Is it possible to use post and get function in one php form?
Is it possible to use post and get function in one php form?

Time:04-14

<html>
<body>
    <div  >
        <fieldset>
        <div>
            <h1>Student Details</h1>
        </div>
        <form method="">
        <table>
        <tr>
            <td><label>Student ID:</label></td>
            <td><input type="text" name="StuID"></td>
            <td><input type="submit" name="find" value="Find"></td>
        </tr>
        <tr><td></td></tr>
        <tr>
            <td><label>Student IC:</label></td>
            <td><input type="text" name="StuIC"></td>
        </tr>
        <tr>
            <td><label>School ID:</label></td>
            <td><input type="text" name="SchID"></td>
        </tr>
        <tr>
            <td><label>Student Name:</label></td>
            <td><input type="text" name="StuNAME"></td>
        </tr>
        <tr>
        <tr>
            <td><label>Gender:</label></td>
            <td>
                <select name="Gender">
                    <option>--Gender--</option>
                    <option>Male</option>
                    <option>Female</option>
                </select>
            </td>
        </tr>
        <tr>
            <td><label>Nationality:</label></td>
            <td><input type="text" name="Nationality"></td>
        </tr>
        <tr>
            <td><label>Race:</label></td>
            <td>
                <select name="Race">
                    <option>--Race--</option>
                    <option>Malay</option>
                    <option>Indian</option>
                    <option>Chinese</option>
                    <option>Other</option>
                </select>
            </td>
        </tr>
        <tr>
            <td><label>Religion:</label></td>
            <td>
                <select name="Religion">
                    <option>--Religion--</option>
                    <option>Muslim</option>
                    <option>Buddist</option>
                    <option>Hindu</option>
                    <option>Christian</option>
                    <option>Other</option>
                </select>
            </td>
        </tr>
        <tr>
            <td><label>Telephone Number:</label></td>
            <td><input type="text" name="TelNo"></td>
        </tr>
        </tr>
        <tr>
            <td><input type="submit" name="add" value="Add"></td>
            <td><input type="submit" name="update" value="Update"></td>
            <td><input type="submit" name="delete" value="Delete"></td>
        </tr>
    </table>
    </form>
    </fieldset>
    </div>
</body>
</html>

This is my form, the user can find the data by insert the ID and click "Find" and the data will display in the form. With the same form, user can update the data with just change data in the form and click "Update" and the data will update in database.

Is it possible for me to build a form that can do Insert, Update and Delete using just one php form.

CodePudding user response:

You can create one form, and with JS/jQuery (or any other lib) submit that form via Ajax depending on button clicked:

<form id="my-form">
    ...
    <tr>
        <td><label>Student ID:</label></td>
        <td><input type="text" name="StuID" id="stu-id"></td>
        <td><input type="submit" name="find" value="Find" onClick="sendPost()"></td>
    </tr>
    <tr>
        <td><input type="button" name="add" value="Add" onClick="sendPost()"></td>
        <td><input type="button" name="update" value="Update" onClick="sendPut()"></td>
        <td><input type="button" name="delete" value="Delete" onClick="sendDelete()"></td>
    </tr>
</form>
function sendFind() {
    $.ajax({
         url: 'example.com/my-action?StuID='   $('#stu-id').val(),
         method: 'GET',
         success: function () {}
         ...
    });
}


function sendPut() {
    $.ajax({
         url: 'example.com/my-action',
         method: 'PUT',
         dataType: 'json',
         data: $("#my-form").serialize(),
         success: function () {}
         ...
    });
}


function sendDelete() {
    $.ajax({
         url: 'example.com/my-action',
         method: 'DELETE',
         dataType: 'json',
         data: $("#my-form").serialize(),
         success: function () {}
         ...
    });
}

As you can see there even can be same URL and act differently depending on request type.

CodePudding user response:

Its not possible though you can use $_REQUEST in php to get any posted data whether with POST or GET. For your case you can name your input type submit with different names then check in your php

 <?php
    if(isset($_POST['insert'])){ //insert functionality }
    if(isset($_POST['update'])){ //update functionality }
    if(isset($_POST['delete'])){ //delete functionality }
   ?>
  • Related