简体   繁体   中英

Post Prestashop Form with Ajax - Tools::getValue()

Hello I wanna save a form into prestashop database with ajax, but I encounter some difficulties. Let me show you my try. HTML Form:

<form action="" method="POST" class="">
    <textarea name="question_content" row="4" class="form-field"></textarea>
    <button type="submit" name="saveQuestion" id="question_ajax_save" class="button-standard">Frage abschicken</button>
</form>

JS:

$("#question_ajax_save").click(function(event) {
    event.preventDefault();
    var question = $("input[name=question_content]").val();
    if (question !==''){
        $.ajax({
            type: 'POST',
            url: baseUri + 'modules/genzo_questions/ajax.php',
            data: {
                save_question: 1,
            },
            datatype: 'json',
            success: function (response) {

                response = $.parseJSON(response);

                if (response.status === false){
                    // Do something
                }
                else {
                    // Do something
                }
            }
        });
    }
});

PHP Bridge:

if (Tools::getValue('save_question')==1) {
    echo json_encode($genzo_question->ajaxSaveQuestion());
}

In my method ajaxSaveQuestion() I want to use Tools::getValue('question_content') . But it's empty. Why is this? I could send it with "data:" but in my method I need Tools:getValue('id_product') too, which is also empty.

In short: How can I use Tools::getValue('') with Ajax Post?

you need to add this in your ajax.php file :

require_once dirname(__FILE__) . '/../../config/config.inc.php';
require_once dirname(__FILE__) . '/../../init.php';

Then you can use prestashop functions like : Tools::getValue('')

I think that you forgot to add question_content in your code :

$("#question_ajax_save").click(function(event) {
    event.preventDefault();
    var question = $("input[name=question_content]").val();
    if (question !==''){
        $.ajax({
            type: 'POST',
            url: baseUri + 'modules/genzo_questions/ajax.php',
            data: {
                save_question: 1,
                question_content: question
            },
            datatype: 'json',
            success: function (response) {

                response = $.parseJSON(response);

                if (response.status === false){
                    // Do something
                }
                else {
                    // Do something
                }
            }
        });
    }
});

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