简体   繁体   中英

Drupal how to define page for AJAX form POST

I am trying to send a form via Ajax, but can not figure out how to set the receiver to be a PHP script. When trying to pass thee data I get 404 page not found. I do not know how to define the PHP script where I want the data to be sent.

I tried defining the script path at the start of the page

<?php
   module_load_include('php', 'mysite', 'modules/test/customer');
?>

AJAX part

$(document).ready(function() {

    // process the form
    $('form').submit(function(event) {

        // get the form data
        // there are many ways to get this data using jQuery (you can use the class or id also)
        var formData = {
            'id'                : $('input[name=mav_id]').val(),
            'sku'           : $('input[name=sku1]').val(),
        };

        // process the form
        $.ajax({
            type        : 'POST', // define the type of HTTP verb we want to use (POST for our form)
//redining the base path
            url: Drupal.settings.basePath + "modules/test/customer" ,
            data        : formData, // our data object
            dataType    : 'json', // what type of data do we expect back from the server
                        encode          : true
        })
            // using the done promise callback
            .done(function(data) {

                // log data to the console so we can see
                console.log(data); 

                // here we will handle errors and validation messages
            });

        // stop the form from submitting the normal way and refreshing the page
        event.preventDefault();
    });

});

HTML form

   <table>
        <tr><th align="LEFT"><b><i>Cu data</b></i></th></tr>
        <form action="modules/test/customer.php" method="post">
        <div class='cu'>
         <tr><td>Mav ID: <input type="text" name="mav_id"></td>
        <td>sku: <input type="text" name="sku1"></td>
         <tr> <td><input type="submit" value="Submit"></td> </tr>

I still get this error:

could not find the site you requested "/node/modules/test/customer.php

How can I get rid of the node part and get the script to send the data to right address?

remove the below code form page callback function

module_load_include('php', 'mysite', 'modules/test/customer');

and add it to hook_init

it will solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM