Jump to content

Recommended Posts

Hi!

So I've got I group of php code for a form. Below:

<?php
  $full_name = filter_input[INPUT_POST, 'full_name'];
  $phone = filter_input[INPUT_POST, 'phone'];
  $email = filter_input[INPUT_POST, 'email'];
  $type-of-property = filter_input(INPUT_POST, 'type-of-property’);
  $property_address = filter_input[INPUT_POST, 'property_address '];
  $price_range = filter_input[INPUT_POST, ‘price_range’];
  $ideal-number-of-bedrooms = filter_input[INPUT_POST, ‘ideal-number-of-bedrooms’]; 
  $ideal-number-of-bathrooms = filter_input[INPUT_POST, ‘ideal-number-of-bathrooms’]; 
  $are-you-preapproved-for-a-mortgage = filter_input[INPUT_POST, ‘are-you-preapproved-for-a-mortgage’]; 
if ($are-you-preapproved-for-a-mortgage = yes )  {echo “$Pre-approvalAmount";}
  $type-of-parking-you-need = filter_input[INPUT_POST, ‘type-of-parking-you-need’]; 
  $expected-timeline-for-buying = filter_input[INPUT_POST, ‘expected-timeline-for-buying’]; 
  $neighborhoods-or-zip-codes-youd-like-to-explore = filter_input[INPUT_POST, ‘neighborhoods-or-zip-codes-youd-like-to-explore’]; 
  $what-do-you-want-the-most-in-your-dream-home = filter_input[INPUT_POST, ‘what-do-you-want-the-most-in-your-dream-home’]; 

And the only error that's come up consistently is Parse error: syntax error, unexpected token ",", expecting "]" in file on line 2
Errors parsing file.

It's confusing because only that line has come up in error, when the formatting seems to be the same as the other lines. Help please 

Link to comment
https://forums.phpfreaks.com/topic/317017-parse-error-syntax-error/
Share on other sites

You are using brackets to wrap your function args.  Use parens.

And - I would not use dashes for varnames.  Use underscores.

And  - A comparison (in an if) uses 2 = signs, not just one.

And - you are going to get real tired of re-typing var names that are way too long pretty quickly.  I know you are trying to be clear but perhaps you could re-think your names to make it easier to type them and read them later on.

Edited by ginerjm
  • Like 1

Maybe something like this for variable naming and form field names?

$fullName = filter_input(INPUT_POST, 'fullName');
$phone = filter_input(INPUT_POST, 'phone');
$email = filter_input(INPUT_POST, 'email');
$propertyType = filter_input(INPUT_POST, 'propertyType');
$address = filter_input(INPUT_POST, 'address');
$priceRange = filter_input(INPUT_POST, 'priceRange');
$bedrooms = filter_input(INPUT_POST, 'bedrooms'); 
$bathrooms = filter_input(INPUT_POST, 'bathrooms'); 
$preApproved = filter_input(INPUT_POST, 'preApproved'); 

if ($preApproved === "yes")  {
    echo $preApprovalAmount;
}

$parkingType = filter_input(INPUT_POST, 'parkingType'); 
$buyingTimeline = filter_input(INPUT_POST, 'buyingTimeline'); 
$exploreZips = filter_input(INPUT_POST, 'exploreZips'); 
$dreamHomeWish = filter_input(INPUT_POST, 'dreamHomeWish');

 

  • Like 1

No matter what, by passing no third parameter to the filter_input function no filtering will actually happen and all of this is pointless. From the docs:

 filter_input(
    int $type,
    string $var_name,
    int $filter = FILTER_DEFAULT,
    array|int $options = 0
): mixed

and

Quote

filter

The ID of the filter to apply. The Types of filters manual page lists the available filters.

If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. This will result in no filtering taking place by default.

It's not clear what's happening with the submitted data (except for the one random output of $preApprovalAmount), but the OP needs to actually handle the input in a manner appropriate for the usage. If it's for a DB query, use prepared statements. If it's for output, use htmlspecialchars() or htmlentities().

also, since filter_input() hides undefined index errors, i.e. typo mistakes between form field names and the php code, it should be avoided.

if you are just starting out, get your code to work completely for one form field of one type, e.g. a text field, then add and test the code needed for other types of fields, one at a time. once you have determined how to handle each different type of field, you can worry about the code needed for all the  rest of the fields. by writing out code for 13+ fields, before you have working code for even one field, you will waste a huge amount of time making corrections to all that code.

you should handle first and last names separately, so that you can distinguish between the two, e.g. is someone Ross Martin, or Martin Ross?

other than trimming data, mainly so that you can determine if it is all white-space characters, you should NOT alter input data. just validate the trimmed data. if the data is valid, use it. if the data is not valid, setup an error message for the user letting them know what is wrong with each piece of data, let them correct the problem, and resubmit the data.

you should NOT write out line after line of code creating discrete variables for each field. this is just a waste of typing. instead, keep the form data as a set, in a php array variable, then operate on elements in this array variable throughout the rest of the code. once you do this, you can trim all the data at once, using one single line of code.

 

Thank you all for the very helpful replies! I am intrigued by the idea of shortening the var names. Perhaps it would help if I would explain more on what the PHP is for. I've created HTML forms and I want the user's information that is collected by the fields to be submitted and sent to an email address.

As for the idea of shortening the var names, I wanted to be clear and precise because I wasn't sure how specific the var names in PHP had to be. Here's some HTML for the forms if this also helps to understand what I'm trying to accomplish

</div>


<div class="uacf7-form-284"><label> Full Name*

<span class="wpcf7-form-control-wrap" data-name="full-name"><input size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false" value="" type="text" name="full-name" /></span> </label>



<label> Phone*

<span class="wpcf7-form-control-wrap" data-name="tel-838"><input size="40" class="wpcf7-form-control wpcf7-text wpcf7-tel wpcf7-validates-as-required wpcf7-validates-as-tel" aria-required="true" aria-invalid="false" value="" type="tel" name="tel-838" /></span> </label>



<label> Email*

<span class="wpcf7-form-control-wrap" data-name="your-email"><input size="40" class="wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-required wpcf7-validates-as-email" autocomplete="email" aria-required="true" aria-invalid="false" value="" type="email" name="your-email" /></span> </label>



<label> Type of property*

<span class="wpcf7-form-control-wrap" data-name="Type-of-Property"><select class="wpcf7-form-control wpcf7-select wpcf7-validates-as-required" aria-required="true" aria-invalid="false" name="Type-of-Property"><option value="Lot">Lot</option><option value="Single Family">Single Family</option><option value="Multi-Unit">Multi-Unit</option><option value="Investment">Investment</option><option value="Commercial">Commercial</option></select></span> </label>



<label> Property Address*

<span class="wpcf7-form-control-wrap" data-name="property-address"><input size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false" value="" type="text" name="property-address" /></span> </label>



<label> Price Range*

<span class="wpcf7-form-control-wrap" data-name="PriceRange"><input size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false" value="" type="text" name="PriceRange" /></span> </label>



<label> Ideal Number of Bedrooms*

<span class="wpcf7-form-control-wrap" data-name="Ideal-Number-of-Bedrooms"><input class="wpcf7-form-control wpcf7-number wpcf7-validates-as-number" max="6" aria-invalid="false" value="0" type="number" name="Ideal-Number-of-Bedrooms" /></span> </label>



<label> Ideal Number of Bathrooms*

<span class="wpcf7-form-control-wrap" data-name="Ideal-Number-of-Bathrooms"><input class="wpcf7-form-control wpcf7-number wpcf7-validates-as-number" max="6" aria-invalid="false" value="0" type="number" name="Ideal-Number-of-Bathrooms" /></span> </label>



<label> Are you preapproved for a mortgage?*

<span class="wpcf7-form-control-wrap" data-name="Are-you-preappoved-for-a-mortgage"><span class="wpcf7-form-control wpcf7-radio"><span class="wpcf7-list-item first"><label><input type="radio" name="Are-you-preappoved-for-a-mortgage" value="Yes" /><span class="wpcf7-list-item-label">Yes</span></label></span><span class="wpcf7-list-item last"><label><input type="radio" name="Are-you-preappoved-for-a-mortgage" value="No" /><span class="wpcf7-list-item-label">No</span></label></span></span></span> </label>



<div data-id="Pre-approvalAmount" data-orig_data_id="Pre-approvalAmount" data-class="wpcf7cf_group"><label>Pre-Approval Amount

<span class="wpcf7-form-control-wrap" data-name="Pre-approvalAmount"><input size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false" value="" type="text" name="Pre-approvalAmount" /></span></label></div>

 

Var names are strictly up to the writer.  He/she is the only one who will ever see them.  But that also means that down the road someone else could inherit this project and have to make sense out of your logic.  So - make the names meaningful but learn to use shorter words or abbreviations that will make sense to the next person.  Comments are also helpful in code to pass along your thoughts to the next coder as well.  As I see in your latest post Class names could also be shortened.  They don't have to be so 'informative' since everyone knows what they are.  If you keep typing such long names in your projects, at some point in your future you will begin to wonder why you ever started out this way.  :)

One thing I don't think was mentioned is to be wary of smart / curly quotes. I'm guessing those are remnants from copying code from a blog post. Be aware that PHP requires straight quotes.

The following, for example,

expected-timeline-for-buying

should be

'expected-timeline-for-buying'
  • Great Answer 1
4 hours ago, cyberRobot said:

One thing I don't think was mentioned is to be wary of smart / curly quotes. I'm guessing those are remnants from copying code from a blog post. Be aware that PHP requires straight quotes.

The following, for example,

expected-timeline-for-buying

should be

'expected-timeline-for-buying'

@cyberRobot I'm new to PHP coding your smart/curly quote vs straight quote looks exactly the same to me (minus the color of the text), so I'm sorry but I'm not sure I understand what the difference is in this instance

The little backtick located above the tab key is not a valid 'quote' character for php coding ( and probably a lot of other coding).  Use either double quotes or single quotes - both on the same key next to the enter key.  The backticks are used in sql coding to identify field names, if necessary

17 hours ago, jgardonis said:

smart/curly quote vs straight quote looks exactly the same

Perhaps you already got this by Barand's response. If not, maybe this will help:

Curly:    ‘ ’
Straight: ' '

I should also mention that your original code posted at the top of this thread also had a curly double quote.

Curly:    “ ”
Straight: " "

Hopefully, you can see the curve in the curly quotes. Of course, the look of curly quotes are different based on the font used, as you may have noticed with Barand's response.

  • 2 months later...

Well, there are some logical errors with your code, you can try this code to fix this error.

<?php
  $full_name = filter_input(INPUT_POST, 'full_name');
  $phone = filter_input(INPUT_POST, 'phone');
  $email = filter_input(INPUT_POST, 'email');
  $type_of_property = filter_input(INPUT_POST, 'type-of-property');
  $property_address = filter_input(INPUT_POST, 'property_address');
  $price_range = filter_input(INPUT_POST, 'price_range');
  $ideal_number_of_bedrooms = filter_input(INPUT_POST, 'ideal-number-of-bedrooms'); 
  $ideal_number_of_bathrooms = filter_input(INPUT_POST, 'ideal-number-of-bathrooms'); 
  $are_you_preapproved_for_a_mortgage = filter_input(INPUT_POST, 'are-you-preapproved-for-a-mortgage'); 
  if ($are_you_preapproved_for_a_mortgage == 'yes') {
    echo $Pre-approvalAmount; // Make sure $Pre-approvalAmount is defined elsewhere
  }
  $type_of_parking_you_need = filter_input(INPUT_POST, 'type-of-parking-you-need'); 
  $expected_timeline_for_buying = filter_input(INPUT_POST, 'expected-timeline-for-buying'); 
  $neighborhoods_or_zip_codes_you'd_like_to_explore = filter_input(INPUT_POST, 'neighborhoods-or-zip-codes-youd-like-to-explore'); 
  $what_do_you_want_the_most_in_your_dream_home = filter_input(INPUT_POST, 'what-do-you-want-the-most-in-your-dream-home'); 
?>

 

Thanks

 

 

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.