0

I just want to ask a simple question. I am creating a validation form using PHP. And what I need to do is to get the value of my textbox and assign it to a variable. Because I need that variable for comparing. But this process is done without an action form like GET or POST. How can I create a simple jquery for this?

Here's my sample flow.

<?php

    $data_qty = fn_product_qty($product_id);
    $x = //should be the textbox qty

    if($x != $data_qty){
       //some code here....
    }
?>
.....
<input type='text' name='qty' value='1' />
4
  • use ajax to send the data and get on PHP Commented Apr 25, 2014 at 9:25
  • 1
    php is executed on the server, you can't pass a javascript variable to php without reloading the page Commented Apr 25, 2014 at 9:26
  • so it is not possible? I am thingking of using a session but I don't have an idea. Commented Apr 25, 2014 at 9:26
  • Either use AJAX to perform a server-side validation or try and move the validation to the client-side. Commented Apr 25, 2014 at 9:26

2 Answers 2

1

You can use ajax to do this. Can do alot of things, using jquery validation. Usually when i used to compare username exists in my server-side db i extend a method from jquery validation. Glad to post it here if u'd like

   $.ajax({
                        url: "/Mycheckingaction",
                        type: 'POST',
                        dataType: 'json',
                        data: { id: $('input[name="qty"]').val(); },
                        success: function (response, textStatus, xhr) {
                                 if(!response)
                                 {
                                     alert('Error,not equal');     
                                 }
         });
Sign up to request clarification or add additional context in comments.

Comments

1

It can be done in javascript like,

<script>
    var x='<?php echo $x;?>';
    $('button').on('click',function(){
        var qty=$('input[name="qty"]').val();
        if(x!=qty){
           alert('Both are not equal');
        }
    });
</script>

If you want in server side then use jquery ajax and php $_SESSION to compare both values.

Comments

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.