Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/226011-passing-users-input-value-to-a-variable/
Share on other sites

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

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.

$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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.