0

I am trying to cross check some values with what I have in the database before letting those data go any further.

I have a form and in this form I am post

pid, Name, size, category and qty.

I have a validation already for the qty.

In those data I am posting I am using those to find the price in the database. I AM NOT POSTING THE PRICE.

Because I am using those post data to find the price someone might want to mess about with them for an example change the category or size value to whatever. If they do this, it will still get posted and the page will render and it will look like

Name:     qty:      unit price:         total item price:
size:

Although those table name shows NOTHING is getting shown which means the page is just rendering for nothing. I know some of you might think "if the person whats to mess about with the values, that is their fault" and I totally agree but I still want to stop the page from rendering

if everything posted doesn't relate to any of the item with that product ID (Pid).

How can I use ajax to validate them before rendering it in the other page?

I already Have an ajax which looks like

$(document).ready(function(){
    $('#selected').hide();
    $('#button').click(function(){
        var pid = $('#pid').val();
        var size = $('#size').val();
         var qty = $('#Qty').val();
        var price = '\u00A3' + parseInt($('#pricetag').text().replace(/^\D/, ''), 10) * qty;
        if (!/^[1-9]\d?$/.test(Qty)){
            alert('Quantity should not be below 1 or null');
            return false; // don't continue
        } 
        else {
        $('#sprice').text(price);
        $('#ssize').text(size);
        $('#selected').slideDown();
        }
        $.ajax({
            url: 'cart.php',
            type: 'POST',
            data: { pid:pid, 
            size:size, 
            Qty:Qty, 
            Category:Category },
            success: function(data)
            {

            }
        });
    });
});

in cart.php

is where I am posting those values to.

How can I do this please?

5
  • How can you use ajax to validate this stuff? its the same thing as without it, but via XmlHttpRequest object only - the only difference how it will be "exchanged". That's all nothing more (hint). As form form: If it's not okay you want to show error messages, otherwise you want to show kinda success modal dialog, right? Commented Jul 26, 2013 at 1:35
  • @DaveJust yes if its not okay it should an error message as an alert if its okay it should add to the cart. Commented Jul 26, 2013 at 21:38
  • 1) You don't handle data parameter in success: function(data) 2) !/^[1-9]\d?$/.test(Qty) <-- you are doing it wrong. Just disable JS in your browser and you will feel that too Commented Jul 27, 2013 at 19:10
  • @DaveJust i am doing a server side validation too with a php... I know JavaScript can be turn off but thanks for bring it up anyways Commented Jul 27, 2013 at 19:27
  • Are you implementing Model-View-Controller or Model-View-Presenter in you application with a router? I'm saying this, because in that case you can simply add ajaxAction() method to validate a form, then send a response as you've described above. Commented Jul 27, 2013 at 22:05

1 Answer 1

1

You want to ask the server to see if the data is valid. Aside from the form data, I would also post a flag telling PHP that this is the AJAX validation so it can tell the AJAX post from normal form submission.

In PHP, after the data validity is determined, it can just return true or false. Then your Javascript can determine what to do from that point on.

However, I think there are other ways to go about this. Maybe you shouldn't let the user modify these fields, or maybe you can post the form, and render the form again if the user has submitted invalid data. Another alternative is to limit the choices that the user can do, so their input is always valid.

Sign up to request clarification or add additional context in comments.

8 Comments

everything in the form is a select option the only input the user can type in is the Quantity which I have a javascript validation for that make make sure that its only a whole number is entered.
if u right click on chrome and select inspect element you can modify things in that. for an example change a form value, they is no way to stop someone from doing that Unless you disable right clicks on your website, which is not user friendly and not really highly recommended.
@SarahJames If your users go that far, then they can also modify your JavaScript. You should always verify all user input on the server side, but I think it's okay let users who want to break things to break them. They could even build their own browser if they like.
I know that the quantity shouldnt be letters or have a . . In my database size is stored as(for an example) 12 inch and in the value it is shown as 12 inch because I am echoing it out... if I use this jqery validation ` if (!/^[1-9]\d?$/.test(size)){ alert('please select an option'); return false; } ` it doesn't work. How can I validate that please
@SarahJames I thought you wanted to validate this on the server. Send the text to the server and do your check against the database. You should not do client side validation on inputs that the user has modified in the developer tools. It's absurd for any user to expect your application to work correctly after they have messed it up. Thus, you should do server side validation, but not client side validation for select options.
|

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.