I made admin menu inside wordpress and I have form that allows admin to insert some values. I have html:

 <form id="prices_form" method="post" action="">
        <div style=font-weight:bold;font-size:16px;>Location 1</div>
        <input id="location1" name="location1" type="text" />
        <input type="hidden" name="count" value="1" />
        <div style=font-weight:bold;font-size:16px;>Location 2</div>

        <input class="input" id="location2" name="location2" type="text" placeholder="Type something"/>
        <button id="b1" class="btn add-more" type="button">+</button></div>
    <div style=font-weight:bold;font-size:16px;>Price(KN)</div>
    <input type="number" id="price" name="price" min="0" step="0.01"/><br>
    <input id="submit" name="submit" type="submit" value="Save prices" />

And I added this inside my functions.php inside my theme folder:

 function ajax_savePrice_init(){

wp_register_script('ajax-savePrice-script', get_template_directory_uri() . '/ajax-savePrice-script.js', array('jquery') ); 

wp_localize_script( 'ajax-savePrice-script', 'ajax_savePrice_object', array( 
    'ajaxurl' => admin_url( 'admin-ajax.php' ),
    'redirecturl' => home_url(),
    'loadingmessage' => __('Sending user info, please wait...')

// Enable the user with no privileges to run ajax_login() in AJAX
add_action( 'wp_ajax_nopriv_ajaxsavePrice', 'ajax_savePrice' );
add_action('init', 'ajax_savePrice_init');
 function ajax_savePrice(){
        $conn = new mysqli($servername, $username, $password, $dbname);
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        $location1 = $_POST['location1'];
        $location2 = $_POST['location2'];
        $price = $_POST['price'];

        $result = $conn->query("SELECT * FROM prices WHERE location1 = '$location1' AND location2='$location2' OR location1 = '$location2' AND location2='$location1'");
        $row_count = $result->num_rows;
        if ($row_count >= 1) {
            echo 'That locations are already inserted. Do you want to update price?';
        } else {
            $query = "INSERT INTO prices (location1, location2, price) VALUES(?, ?, ?)";
            $statement = $conn->prepare($query);

            //bind parameters for markers, where (s = string, i = integer, d = double,  b = blob)
            $statement->bind_param('ssi', $location1, $location2, $price);

            if ($statement->execute()) {
                print 'Success! ID of last inserted record is : ' . $statement->insert_id . '<br />';
            } else {
                die('Error : (' . $conn->errno . ') ' . $conn->error);


And ajax-savePrice-script.js

jQuery(document).ready(function($) {

// Perform AJAX login on form submit
$('#prices_form').on('submit', function(e){

        type: 'POST',
        dataType: 'json',
        url: ajax_savePrice_object.ajaxurl,
        data: { 
            'action': 'ajaxsavePrice', 
            'location1': $('#location1').val(), 
            'location2': $('#location2').val(), 
            'price': $('#price').val() },
        success: function(data){



Page reloads and nothing happens...

Any hint?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.