0

I want to pass the value of jquery in PHP function is it possible to send the value. like for example I have a textbox and in textbox when the user add 5 then it instantly send to the php function after that when the user add 56 then the value is been send to the PHP function... I was trying but its not working for me.. my code is

<?php
$block->storeInAlgolic();
?>
<script>
    require(
        [
            'jquery',
            'Magento_Ui/js/modal/modal'
        ],
        function(
            $,
            modal

        ){

        $("#order_search").keyup(function(){

            var textinput = $('#order_search').val().substring(0,20);

            <?php $block->getFromAlgolia('textinput'); ?>

            var client = algoliasearch(textinput);

            console.log(client);


            $("#textbox2").val(textinput);
            $("#textbox3").val(textinput);



        });

    });
</script>

<style>
    input[type=number] {
        width: 130px;
        box-sizing: border-box;
        border: 2px solid #ccc;
        border-radius: 4px;
        font-size: 16px;
        background-color: white;
        /*background-image: url('searchicon.png');*/
        background-position: 10px 10px;
        background-repeat: no-repeat;
        padding: 12px 20px 12px 40px;
        -webkit-transition: width 0.4s ease-in-out;
        transition: width 0.4s ease-in-out;
    }

    input[type=number]:focus {
        width: 100%;
    }
</style>
</head>
<body>

<p>Animated search form:</p>

<form type="POST">
    <input type="number" id="order_search" name="searchh" placeholder="Search Order by Order_ID">

    <br /><input type="text" id="textbox2"/><br>
    <br /><input type="text" id="textbox3"/>
</form>

</body>

the output is like enter image description here

is there is any possible way to send Jquery instantly to php function?

0

1 Answer 1

0

STEP 1 Create ajax request to your controller with below code

$("#order_search").keyup(function(){
    var textinput = $('#order_search').val().substring(0,20);                   
    jQuery.ajax({
        url: '<?php echo $this->getUrl('{route}/Index/Index') ?>',
        dataType: 'json',
        data: {
            input_val : textinput
        },
        success: function(data) {
            console.log(data);
        }
    });
});

STEP 2 Create a controller with the above mentioned path with below code

<?php
namespace Vendor\Module\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{

    public function __construct(
        \Magento\Framework\App\Action\Context $context
    ) {
        parent::__construct($context);
    } 

    public function execute(){
        $post = $this->getRequest()->getParam('input_val');

        //Your logic

        $response = $this->resultFactory
            ->create(\Magento\Framework\Controller\ResultFactory::TYPE_JSON)
            ->setData([
                'status'  => "success"
            ]);

        return $response;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.