Hall of Famer Posted January 29, 2011 Share Posted January 29, 2011 Well this may sound confusing. I tried to findways to allow users to input an integer value and then assign it to a variable called $quantity, but all I could find from the internet was the usage of forms. Do I have to use forms, or can I just try this this simple syntax: $quantity = "<input name='quantity' type='text' id='quantity' size='3' maxlength='3'>"; If I do have to use forms, then how can I ever assign user's input value to a variable? Please help. Quote Link to comment https://forums.phpfreaks.com/topic/226011-passing-users-input-value-to-a-variable/ Share on other sites More sharing options...
dragon_sa Posted January 29, 2011 Share Posted January 29, 2011 top of page <?php if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['quantity']) { if (is_int($_POST['quantity']) { $quantity= $_POST['quantity']; } } ?> <form method='post' action=''> <input name='quantity' type='text' id='quantity' size='3' maxlength='3' /> <input type='submit' name='submit' value='submit' /> </form> Then you can use the variable $quantity anywhere on your page Quote Link to comment https://forums.phpfreaks.com/topic/226011-passing-users-input-value-to-a-variable/#findComment-1166811 Share on other sites More sharing options...
Hall of Famer Posted January 29, 2011 Author Share Posted January 29, 2011 umm so the simple syntax wont work? Is the part submit important? What does it do? Quote Link to comment https://forums.phpfreaks.com/topic/226011-passing-users-input-value-to-a-variable/#findComment-1166833 Share on other sites More sharing options...
dragon_sa Posted January 29, 2011 Share Posted January 29, 2011 The submit is a button that sends your form data back to this page for processing $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['quantity'] checks for the form being submitted and also that the value of quantity has been set is_int($_POST['quantity'] checks to make sure that the value of quantity is an integer $quantity= $_POST['quantity']; sets the variable $quantity to the integer value submitted. For it to work you need to have all the parts of the form as shown. Quote Link to comment https://forums.phpfreaks.com/topic/226011-passing-users-input-value-to-a-variable/#findComment-1166843 Share on other sites More sharing options...
lazylodr Posted January 29, 2011 Share Posted January 29, 2011 $quantity = "<input name='quantity' type='text' id='quantity' size='3' maxlength='3'>"; All you are doing here is assigning the value "<input name='quantity' type='text' id='quantity' size='3' maxlength='3'>" to the variable $quantity in PHP on the server. This has nothing to do with data from a user. If you want data from a user, you must use forms and their submitted values (using $_GET, $_POST variables depending on the form's method attribute) from the request as outlined above. The <input type="submit" /> renders a submit button on the form so the user can send the data back to the server. Quote Link to comment https://forums.phpfreaks.com/topic/226011-passing-users-input-value-to-a-variable/#findComment-1166844 Share on other sites More sharing options...
Pikachu2000 Posted January 29, 2011 Share Posted January 29, 2011 is_int($_POST['quantity'] checks to make sure that the value of quantity is an integer is_int on form data will never return TRUE since all form data is, by default, string type. In this case, since a whole number is expected, ctype_digit would be appropriate to validate it. $quantity= $_POST['quantity']; sets the variable $quantity to the integer value submitted. That would need to be $quantity = (int) $_POST['quantity']; to cast the value as an integer. Quote Link to comment https://forums.phpfreaks.com/topic/226011-passing-users-input-value-to-a-variable/#findComment-1166858 Share on other sites More sharing options...
Hall of Famer Posted January 30, 2011 Author Share Posted January 30, 2011 Thank you so much for the reply guys, I will give a try tomorrow and see if this works. Quote Link to comment https://forums.phpfreaks.com/topic/226011-passing-users-input-value-to-a-variable/#findComment-1167278 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.