Jump to content

Recommended Posts

I have been using PHP for quite some time and I always make these forms and the first thing i have to do on the script page is to call each form field a variable to use it later.  Is there a better way than this.  In other words to take a collection of all the form fields and not have to call them.

 

What do people do when they have forms with 100 fields?

 

Thanks in advance

 

Chris

Link to comment
https://forums.phpfreaks.com/topic/123679-making-post-variables-easier/
Share on other sites

you don't have to assign post variables to new variables, since they already exist in the $_POST array. automatically assigning variables for whatever someone submits is dangerous. that's why register_globals is turned OFF by default. if you want to create a security issue, you can turn it ON in php.ini.

So in other words.

 

If you have a form:

<form id="form1" name="form1" method="post" action="post.php">

  <label>

  <input type="text" name="textfield1" id="textfield1" />

  </label>

  <p>

    <label>

    <input type="text" name="textfield2" id="textfield2" />

    </label>

  </p>

  <p> </p>

</form>

 

When you get over to post.php

 

you are just inputting the data using the $_POST['textfield1'] rather than assigning it a variable?

 

i.e.

<?

echo("This is my first text field" . $_POST['textfield1']);

echo("<br>");

echo("this is my second text field" . $_POST['textfield2']);

?>

 

Thanks

Well, using any kind of non-validated data is dangerous.  It doesn't matter if you're using it like:

$query = "INSERT INTO my_dbtable (my_column) VALUE ('". $_POST['someField'] ."')");
$result = mysql_query($query);

 

Or:

$someField = $_POST['someField'];

$query = "INSERT INTO my_dbtable (my_column) VALUE ('$someField')");
$result = mysql_query($query);

 

Neither one is innately more secure than the other.  In fact, both are dangerously insecure if left as is.  Remember: all incoming data is potentially bad.

 

I tend to assign scrubbed (i.e., validated) data to variables because it makes it easier on me to refer to them by a simple variable name than to deal with the cumbersome superglobal syntax every time I want to use form data.

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.