1

I'm am using below code to insert data int my WordPress data base. This code is not working properly, When i insert data it shows the same page and data is not being inserted into database.

<?php get_header(); ?>
<div class="content">
    <form>
        Job Title: <input type="text" name="jtitle"><br><br>
        Job Link: <input type="text" name="jlink"><br><br>
        Job Last date: <input type="text" name="jld"><br><br>
        <input type="submit" value="Add Job" name="insert"><br><br>
    </form>
    <?php
    if (isset($_POST['insert'])) {
        $jt=$_POST['jtitle'];
        $jl=$_POST['jlink'];
        $jld=$_POST['jld'];

        global $wpdb;
        $sql=$wpdb->insert("wp_job",array("jtitle"=>$jt,"jlink"=>$jl,"jld"=>$jld));
        if ($sql==true) {
            echo "<script>alert('New Job Added')</script>";
        }
        else {

            echo "<script>alert('New Job Not Added!')</script>";
        }
    }
?>
</div><!-- .content /-->
rajat.gite
  • 450
  • 2
  • 10

4 Answers4

1

You are submitting the form data using the default HTTP GET method, not the POST method. As your HTML form tag does not specify the submission method, it defaults to GET not POST.

Either change the form tag in HTML to <form method="POST"> or in your PHP change the $_POST values to $_GET.

Here is a bit more documentation to HTML 5 form if you need to https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-action

PS: you dont need to check the variable $_POST['insert'] whether its set for submission. You can just do isset($_POST) to see whether any form data has been submitted.

Good luck!

user3840170
  • 26,597
  • 4
  • 30
  • 62
TomTom
  • 1,113
  • 1
  • 10
  • 20
1

You are submitting form without specifying method attribute, you should define method="post" for getting data by isset($_POST['insert']) or also you can fetch all data using $_REQUEST, but I recommended you to define action and method attribute.

Here also you can see that the default form method is GET What is the default form HTTP method?

Good Luck Thanks

0

You need to add "action" attribute with value "Post" in form tag

<div class="content">
<?php
    if (isset($_POST['insert'])) {
        global $wpdb;
        $wp_job = $wp_prefix.'job';
        $jt = $_POST['jtitle'];
        $jl = $_POST['jlink'];
        $jld = $_POST['jld'];
        $sql = $wpdb->insert( $wp_job , array( "jtitle"=>$jt, "jlink"=>$jl, "jld"=>$jld ) );
        if ($sql==true) {
            echo "<script>alert('New Job Added')</script>";
        }
        else {
            echo "<script>alert('New Job Not Added!')</script>";
        }
    }
?>
<form method="post" action="">
    Job Title: <input type="text" name="jtitle"><br><br>
    Job Link: <input type="text" name="jlink"><br><br>
    Job Last date: <input type="text" name="jld"><br><br>
    <input type="submit" value="Add Job" name="insert"><br><br>
</form>
</div><!-- .content /-->

Tested and works. Thanks

rajat.gite
  • 450
  • 2
  • 10
0

If you are working in WordPress you need to handling POST requests the WordPress Way

From:
<form> 

To:
<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" 
method="post">

/* add this line somewhere in between the <form> tag. */
   <input type="hidden" name="action" value="job_form">

/* Open up functions.php of your current theme and add these lines in: */

function addNewJob() {
 /**
* At this point, $_GET/$_POST variable are available
 * eg: $jld = $_POST['jld'];
 *
* We can do our normal processing here
 */ 

}
add_action( 'admin_post_nopriv_job_form', 'addNewJob` );
add_action( 'admin_post_job_form', 'addNewJob` );