Rectangle 27 12

Short open tags and <?xml headers in php scripts

T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.

This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:

Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping \" double quotes, or \' single quotes - depending on which was used as string enclosure.

  • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.
  • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.
  • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.

If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:

echo "Some text", $a_variable, "and some runaway string ;
success("finished");

It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.

If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:

$text = Something something.. + these ain't quotes;

Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example these is interpreted as constant identifier. But any following text literal is then seen as bareword/T_STRING by the parser.

If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:

func1()
function2();

PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.

This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:

<?xml version="1.0"?>

PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.

A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:

<?php
    print 123;

You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.

<?php
    ;print 123;

The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.

parsing - PHP Parse/Syntax Errors; and How to solve them? - Stack Over...

php parsing debugging syntax-error
Rectangle 27 12

Short open tags and <?xml headers in php scripts

T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.

This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:

Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping \" double quotes, or \' single quotes - depending on which was used as string enclosure.

  • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.
  • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.
  • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.

If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:

echo "Some text", $a_variable, "and some runaway string ;
success("finished");

It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.

If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:

$text = Something something.. + these ain't quotes;

Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example these is interpreted as constant identifier. But any following text literal is then seen as bareword/T_STRING by the parser.

If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:

func1()
function2();

PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.

This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:

<?xml version="1.0"?>

PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.

A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:

<?php
    print 123;

You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.

<?php
    ;print 123;

The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.

parsing - PHP Parse/Syntax Errors; and How to solve them? - Stack Over...

php parsing debugging syntax-error
Rectangle 27 12

Short open tags and <?xml headers in php scripts

T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.

This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:

Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping \" double quotes, or \' single quotes - depending on which was used as string enclosure.

  • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.
  • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.
  • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.

If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:

echo "Some text", $a_variable, "and some runaway string ;
success("finished");

It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.

If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:

$text = Something something.. + these ain't quotes;

Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example these is interpreted as constant identifier. But any following text literal is then seen as bareword/T_STRING by the parser.

If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:

func1()
function2();

PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.

This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:

<?xml version="1.0"?>

PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.

A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:

<?php
    print 123;

You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.

<?php
    ;print 123;

The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.

parsing - PHP Parse/Syntax Errors; and How to solve them? - Stack Over...

php parsing debugging syntax-error
Rectangle 27 12

Short open tags and <?xml headers in php scripts

T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.

This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:

Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping \" double quotes, or \' single quotes - depending on which was used as string enclosure.

  • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.
  • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.
  • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.

If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:

echo "Some text", $a_variable, "and some runaway string ;
success("finished");

It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.

If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:

$text = Something something.. + these ain't quotes;

Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example these is interpreted as constant identifier. But any following text literal is then seen as bareword/T_STRING by the parser.

If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:

func1()
function2();

PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.

This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:

<?xml version="1.0"?>

PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.

A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:

<?php
    print 123;

You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.

<?php
    ;print 123;

The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.

parsing - PHP Parse/Syntax Errors; and How to solve them? - Stack Over...

php parsing debugging syntax-error
Rectangle 27 12

Short open tags and <?xml headers in php scripts

T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.

This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:

Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping \" double quotes, or \' single quotes - depending on which was used as string enclosure.

  • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.
  • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.
  • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.

If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:

echo "Some text", $a_variable, "and some runaway string ;
success("finished");

It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.

If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:

$text = Something something.. + these ain't quotes;

Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example these is interpreted as constant identifier. But any following text literal is then seen as bareword/T_STRING by the parser.

If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:

func1()
function2();

PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.

This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:

<?xml version="1.0"?>

PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.

A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:

<?php
    print 123;

You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.

<?php
    ;print 123;

The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.

parsing - PHP Parse/Syntax Errors; and How to solve them? - Stack Over...

php parsing debugging syntax-error
Rectangle 27 12

Short open tags and <?xml headers in php scripts

T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.

This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:

Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping \" double quotes, or \' single quotes - depending on which was used as string enclosure.

  • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.
  • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.
  • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.

If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:

echo "Some text", $a_variable, "and some runaway string ;
success("finished");

It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.

If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:

$text = Something something.. + these ain't quotes;

Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example these is interpreted as constant identifier. But any following text literal is then seen as bareword/T_STRING by the parser.

If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:

func1()
function2();

PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.

This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:

<?xml version="1.0"?>

PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.

A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:

<?php
    print 123;

You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.

<?php
    ;print 123;

The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.

parsing - PHP Parse/Syntax Errors; and How to solve them? - Stack Over...

php parsing debugging syntax-error
Rectangle 27 12

Short open tags and <?xml headers in php scripts

T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.

This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:

Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping \" double quotes, or \' single quotes - depending on which was used as string enclosure.

  • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.
  • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.
  • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.

If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:

echo "Some text", $a_variable, "and some runaway string ;
success("finished");

It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.

If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:

$text = Something something.. + these ain't quotes;

Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example these is interpreted as constant identifier. But any following text literal is then seen as bareword/T_STRING by the parser.

If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:

func1()
function2();

PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.

This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:

<?xml version="1.0"?>

PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.

A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:

<?php
    print 123;

You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.

<?php
    ;print 123;

The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.

parsing - PHP Parse/Syntax Errors; and How to solve them? - Stack Over...

php parsing debugging syntax-error
Rectangle 27 12

Short open tags and <?xml headers in php scripts

T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.

This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:

Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping \" double quotes, or \' single quotes - depending on which was used as string enclosure.

  • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.
  • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.
  • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.

If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:

echo "Some text", $a_variable, "and some runaway string ;
success("finished");

It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.

If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:

$text = Something something.. + these ain't quotes;

Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example these is interpreted as constant identifier. But any following text literal is then seen as bareword/T_STRING by the parser.

If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:

func1()
function2();

PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.

This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:

<?xml version="1.0"?>

PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.

A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:

<?php
    print 123;

You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.

<?php
    ;print 123;

The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.

parsing - PHP Parse/Syntax Errors; and How to solve them? - Stack Over...

php parsing debugging syntax-error
Rectangle 27 11

Short open tags and <?xml headers in php scripts

T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.

This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:

Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping \" double quotes, or \' single quotes - depending on which was used as string enclosure.

  • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.
  • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.
  • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.

If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:

echo "Some text", $a_variable, "and some runaway string ;
success("finished");

It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.

If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:

$text = Something something.. + these ain't quotes;

Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example these is interpreted as constant identifier. But any following text literal is then seen as bareword/T_STRING by the parser.

If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:

func1()
function2();

PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.

This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:

<?xml version="1.0"?>

PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.

A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:

<?php
    print 123;

You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.

<?php
    ;print 123;

The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.

parsing - PHP Parse/Syntax Errors; and How to solve them? - Stack Over...

php parsing debugging syntax-error
Rectangle 27 11

Short open tags and <?xml headers in php scripts

T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.

This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:

Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping \" double quotes, or \' single quotes - depending on which was used as string enclosure.

  • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.
  • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.
  • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.

If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:

echo "Some text", $a_variable, "and some runaway string ;
success("finished");

It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.

If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:

$text = Something something.. + these ain't quotes;

Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example these is interpreted as constant identifier. But any following text literal is then seen as bareword/T_STRING by the parser.

If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:

func1()
function2();

PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.

This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:

<?xml version="1.0"?>

PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.

A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:

<?php
    print 123;

You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.

<?php
    ;print 123;

The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.

parsing - PHP Parse/Syntax Errors; and How to solve them? - Stack Over...

php parsing debugging syntax-error
Rectangle 27 11

Short open tags and <?xml headers in php scripts

T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.

This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:

Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping \" double quotes, or \' single quotes - depending on which was used as string enclosure.

  • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.
  • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.
  • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.

If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:

echo "Some text", $a_variable, "and some runaway string ;
success("finished");

It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.

If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:

$text = Something something.. + these ain't quotes;

Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example these is interpreted as constant identifier. But any following text literal is then seen as bareword/T_STRING by the parser.

If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:

func1()
function2();

PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.

This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:

<?xml version="1.0"?>

PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.

A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:

<?php
    print 123;

You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.

<?php
    ;print 123;

The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.

parsing - PHP Parse/Syntax Errors; and How to solve them? - Stack Over...

php parsing debugging syntax-error
Rectangle 27 11

Short open tags and <?xml headers in php scripts

T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.

This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:

Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping \" double quotes, or \' single quotes - depending on which was used as string enclosure.

  • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.
  • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.
  • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.

If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:

echo "Some text", $a_variable, "and some runaway string ;
success("finished");

It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.

If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:

$text = Something something.. + these ain't quotes;

Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example these is interpreted as constant identifier. But any following text literal is then seen as bareword/T_STRING by the parser.

If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:

func1()
function2();

PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.

This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:

<?xml version="1.0"?>

PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.

A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:

<?php
    print 123;

You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.

<?php
    ;print 123;

The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.

parsing - PHP Parse/Syntax Errors; and How to solve them? - Stack Over...

php parsing debugging syntax-error
Rectangle 27 11

Short open tags and <?xml headers in php scripts

T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.

This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:

Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping \" double quotes, or \' single quotes - depending on which was used as string enclosure.

  • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.
  • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.
  • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.

If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:

echo "Some text", $a_variable, "and some runaway string ;
success("finished");

It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.

If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:

$text = Something something.. + these ain't quotes;

Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example these is interpreted as constant identifier. But any following text literal is then seen as bareword/T_STRING by the parser.

If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:

func1()
function2();

PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.

This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:

<?xml version="1.0"?>

PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.

A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:

<?php
    print 123;

You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.

<?php
    ;print 123;

The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.

parsing - PHP Parse/Syntax Errors; and How to solve them? - Stack Over...

php parsing debugging syntax-error
Rectangle 27 11

Short open tags and <?xml headers in php scripts

T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.

This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:

Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping \" double quotes, or \' single quotes - depending on which was used as string enclosure.

  • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.
  • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.
  • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.

If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:

echo "Some text", $a_variable, "and some runaway string ;
success("finished");

It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.

If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:

$text = Something something.. + these ain't quotes;

Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example these is interpreted as constant identifier. But any following text literal is then seen as bareword/T_STRING by the parser.

If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:

func1()
function2();

PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.

This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:

<?xml version="1.0"?>

PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.

A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:

<?php
    print 123;

You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.

<?php
    ;print 123;

The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.

parsing - PHP Parse/Syntax Errors; and How to solve them? - Stack Over...

php parsing debugging syntax-error
Rectangle 27 11

Short open tags and <?xml headers in php scripts

T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.

This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:

Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping \" double quotes, or \' single quotes - depending on which was used as string enclosure.

  • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.
  • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.
  • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.

If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:

echo "Some text", $a_variable, "and some runaway string ;
success("finished");

It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.

If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:

$text = Something something.. + these ain't quotes;

Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example these is interpreted as constant identifier. But any following text literal is then seen as bareword/T_STRING by the parser.

If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:

func1()
function2();

PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.

This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:

<?xml version="1.0"?>

PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.

A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:

<?php
    print 123;

You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.

<?php
    ;print 123;

The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.

parsing - PHP Parse/Syntax Errors; and How to solve them? - Stack Over...

php parsing debugging syntax-error
Rectangle 27 11

Short open tags and <?xml headers in php scripts

T_STRING is a bit of a misnomer. It does not refer to a quoted "string". It means a raw identifier was encountered. This can range from bare words to leftover CONSTANT or function names, forgotten unquoted strings, or any plain text.

This syntax error is most common for misquoted string values however. Any unescaped and stray " or ' quote will form an invalid expression:

Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping \" double quotes, or \' single quotes - depending on which was used as string enclosure.

  • For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.
  • Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal " double quotes.
  • For lengthier output, prefer multiple echo/print lines instead of escaping in and out. Better yet consider a HEREDOC section.

If you miss a closing " then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:

echo "Some text", $a_variable, "and some runaway string ;
success("finished");

It's not just literal T_STRINGs which the parser may protest then. Another frequent variation is an Unexpected '>' for unquoted literal HTML.

If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:

$text = Something something.. + these ain't quotes;

Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example these is interpreted as constant identifier. But any following text literal is then seen as bareword/T_STRING by the parser.

If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:

func1()
function2();

PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one || or the other.

This is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:

<?xml version="1.0"?>

PHP will see the <? and reclaim it for itself. It won't understand what the stray xml was meant for. It'll get interpreted as constant. But the version will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.

A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:

<?php
    print 123;

You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.

<?php
    ;print 123;

The extra ; semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.

parsing - PHP Parse/Syntax Errors; and How to solve them? - Stack Over...

php parsing debugging syntax-error
Rectangle 27 799

Basic usage of .ajax would look something like this:

<form id="foo">
    <label for="bar">A bar</label>
    <input id="bar" name="bar" type="text" value="" />

    <input type="submit" value="Send" />
</form>
// Variable to hold request
var request;

// Bind to the submit event of our form
$("#foo").submit(function(event){

    // Prevent default posting of form - put here to work in case of errors
    event.preventDefault();

    // Abort any pending request
    if (request) {
        request.abort();
    }
    // setup some local variables
    var $form = $(this);

    // Let's select and cache all the fields
    var $inputs = $form.find("input, select, button, textarea");

    // Serialize the data in the form
    var serializedData = $form.serialize();

    // Let's disable the inputs for the duration of the Ajax request.
    // Note: we disable elements AFTER the form data has been serialized.
    // Disabled form elements will not be serialized.
    $inputs.prop("disabled", true);

    // Fire off the request to /form.php
    request = $.ajax({
        url: "/form.php",
        type: "post",
        data: serializedData
    });

    // Callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        // Log a message to the console
        console.log("Hooray, it worked!");
    });

    // Callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
        // Log the error to the console
        console.error(
            "The following error occurred: "+
            textStatus, errorThrown
        );
    });

    // Callback handler that will be called regardless
    // if the request failed or succeeded
    request.always(function () {
        // Reenable the inputs
        $inputs.prop("disabled", false);
    });

});
.success()
.error()
.complete()
.done()
.fail()
.always()
$(document).ready()
$()
$.ajax().done().fail().always();
// You can access the values posted by jQuery.ajax
// through the global variable $_POST, like this:
$bar = isset($_POST['bar']) ? $_POST['bar'] : null;

You could also use the shorthand .post in place of .ajax in the above JavaScript code:

$.post('/form.php', serializedData, function(response) {
    // Log the response to the console
    console.log("Response: "+response);
});

+1 for the extra jQuery API deprecation details, sanitization tips and PHP server side snippet.

Edited your answer to fix a bug: request was declared as a local var making if (request) request.abort(); never work.

A VERY IMPORTANT note, because I spent/wasted/invested a lot of time trying to use this example. You need to either bind the event inside a $(document).ready block OR have the FORM loaded before the bind is executed. Otherwise, you spend a lot of time trying to figure out WHY in hell the binding isn't called.

Yes, I understand that now. But I found many examples that always put a $(document).ready block around so that the example is self-contained. I wrote the comment for a future user who may, like me, stumble on this and end-up reading the comment thread and this beginner 'tip'

If you are applying this to your own code, note that the 'name' attributes are critical to the inputs otherwise serialize() will skip them.

javascript - jQuery Ajax POST example with PHP - Stack Overflow

php javascript jquery ajax post
Rectangle 27 775

Basic usage of .ajax would look something like this:

<form id="foo">
    <label for="bar">A bar</label>
    <input id="bar" name="bar" type="text" value="" />

    <input type="submit" value="Send" />
</form>
// Variable to hold request
var request;

// Bind to the submit event of our form
$("#foo").submit(function(event){

    // Prevent default posting of form - put here to work in case of errors
    event.preventDefault();

    // Abort any pending request
    if (request) {
        request.abort();
    }
    // setup some local variables
    var $form = $(this);

    // Let's select and cache all the fields
    var $inputs = $form.find("input, select, button, textarea");

    // Serialize the data in the form
    var serializedData = $form.serialize();

    // Let's disable the inputs for the duration of the Ajax request.
    // Note: we disable elements AFTER the form data has been serialized.
    // Disabled form elements will not be serialized.
    $inputs.prop("disabled", true);

    // Fire off the request to /form.php
    request = $.ajax({
        url: "/form.php",
        type: "post",
        data: serializedData
    });

    // Callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        // Log a message to the console
        console.log("Hooray, it worked!");
    });

    // Callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
        // Log the error to the console
        console.error(
            "The following error occurred: "+
            textStatus, errorThrown
        );
    });

    // Callback handler that will be called regardless
    // if the request failed or succeeded
    request.always(function () {
        // Reenable the inputs
        $inputs.prop("disabled", false);
    });

});
.success()
.error()
.complete()
.done()
.fail()
.always()
$(document).ready()
$()
$.ajax().done().fail().always();
// You can access the values posted by jQuery.ajax
// through the global variable $_POST, like this:
$bar = isset($_POST['bar']) ? $_POST['bar'] : null;

You could also use the shorthand .post in place of .ajax in the above JavaScript code:

$.post('/form.php', serializedData, function(response) {
    // Log the response to the console
    console.log("Response: "+response);
});

+1 for the extra jQuery API deprecation details, sanitization tips and PHP server side snippet.

Edited your answer to fix a bug: request was declared as a local var making if (request) request.abort(); never work.

A VERY IMPORTANT note, because I spent/wasted/invested a lot of time trying to use this example. You need to either bind the event inside a $(document).ready block OR have the FORM loaded before the bind is executed. Otherwise, you spend a lot of time trying to figure out WHY in hell the binding isn't called.

Yes, I understand that now. But I found many examples that always put a $(document).ready block around so that the example is self-contained. I wrote the comment for a future user who may, like me, stumble on this and end-up reading the comment thread and this beginner 'tip'

If you are applying this to your own code, note that the 'name' attributes are critical to the inputs otherwise serialize() will skip them.

javascript - jQuery Ajax POST example with PHP - Stack Overflow

php javascript jquery ajax post
Rectangle 27 775

Basic usage of .ajax would look something like this:

<form id="foo">
    <label for="bar">A bar</label>
    <input id="bar" name="bar" type="text" value="" />

    <input type="submit" value="Send" />
</form>
// Variable to hold request
var request;

// Bind to the submit event of our form
$("#foo").submit(function(event){

    // Prevent default posting of form - put here to work in case of errors
    event.preventDefault();

    // Abort any pending request
    if (request) {
        request.abort();
    }
    // setup some local variables
    var $form = $(this);

    // Let's select and cache all the fields
    var $inputs = $form.find("input, select, button, textarea");

    // Serialize the data in the form
    var serializedData = $form.serialize();

    // Let's disable the inputs for the duration of the Ajax request.
    // Note: we disable elements AFTER the form data has been serialized.
    // Disabled form elements will not be serialized.
    $inputs.prop("disabled", true);

    // Fire off the request to /form.php
    request = $.ajax({
        url: "/form.php",
        type: "post",
        data: serializedData
    });

    // Callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        // Log a message to the console
        console.log("Hooray, it worked!");
    });

    // Callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
        // Log the error to the console
        console.error(
            "The following error occurred: "+
            textStatus, errorThrown
        );
    });

    // Callback handler that will be called regardless
    // if the request failed or succeeded
    request.always(function () {
        // Reenable the inputs
        $inputs.prop("disabled", false);
    });

});
.success()
.error()
.complete()
.done()
.fail()
.always()
$(document).ready()
$()
$.ajax().done().fail().always();

@PhilibertPerusse Like with any event binding you obviously need the element to exist in the DOM before trying to bind to it, or if you use a delegated bind.

// You can access the values posted by jQuery.ajax
// through the global variable $_POST, like this:
$bar = isset($_POST['bar']) ? $_POST['bar'] : null;

You could also use the shorthand .post in place of .ajax in the above JavaScript code:

$.post('/form.php', serializedData, function(response) {
    // Log the response to the console
    console.log("Response: "+response);
});

+1 for the extra jQuery API deprecation details, sanitization tips and PHP server side snippet.

Edited your answer to fix a bug: request was declared as a local var making if (request) request.abort(); never work.

A VERY IMPORTANT note, because I spent/wasted/invested a lot of time trying to use this example. You need to either bind the event inside a $(document).ready block OR have the FORM loaded before the bind is executed. Otherwise, you spend a lot of time trying to figure out WHY in hell the binding isn't called.

Yes, I understand that now. But I found many examples that always put a $(document).ready block around so that the example is self-contained. I wrote the comment for a future user who may, like me, stumble on this and end-up reading the comment thread and this beginner 'tip'

If you are applying this to your own code, note that the 'name' attributes are critical to the inputs otherwise serialize() will skip them.

javascript - jQuery Ajax POST example with PHP - Stack Overflow

php javascript jquery ajax post
Rectangle 27 773

Basic usage of .ajax would look something like this:

<form id="foo">
    <label for="bar">A bar</label>
    <input id="bar" name="bar" type="text" value="" />

    <input type="submit" value="Send" />
</form>
// Variable to hold request
var request;

// Bind to the submit event of our form
$("#foo").submit(function(event){

    // Prevent default posting of form - put here to work in case of errors
    event.preventDefault();

    // Abort any pending request
    if (request) {
        request.abort();
    }
    // setup some local variables
    var $form = $(this);

    // Let's select and cache all the fields
    var $inputs = $form.find("input, select, button, textarea");

    // Serialize the data in the form
    var serializedData = $form.serialize();

    // Let's disable the inputs for the duration of the Ajax request.
    // Note: we disable elements AFTER the form data has been serialized.
    // Disabled form elements will not be serialized.
    $inputs.prop("disabled", true);

    // Fire off the request to /form.php
    request = $.ajax({
        url: "/form.php",
        type: "post",
        data: serializedData
    });

    // Callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        // Log a message to the console
        console.log("Hooray, it worked!");
    });

    // Callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
        // Log the error to the console
        console.error(
            "The following error occurred: "+
            textStatus, errorThrown
        );
    });

    // Callback handler that will be called regardless
    // if the request failed or succeeded
    request.always(function () {
        // Reenable the inputs
        $inputs.prop("disabled", false);
    });

});
.success()
.error()
.complete()
.done()
.fail()
.always()
$(document).ready()
$()
$.ajax().done().fail().always();
// You can access the values posted by jQuery.ajax
// through the global variable $_POST, like this:
$bar = isset($_POST['bar']) ? $_POST['bar'] : null;

You could also use the shorthand .post in place of .ajax in the above JavaScript code:

$.post('/form.php', serializedData, function(response) {
    // Log the response to the console
    console.log("Response: "+response);
});

+1 for the extra jQuery API deprecation details, sanitization tips and PHP server side snippet.

Edited your answer to fix a bug: request was declared as a local var making if (request) request.abort(); never work.

A VERY IMPORTANT note, because I spent/wasted/invested a lot of time trying to use this example. You need to either bind the event inside a $(document).ready block OR have the FORM loaded before the bind is executed. Otherwise, you spend a lot of time trying to figure out WHY in hell the binding isn't called.

Yes, I understand that now. But I found many examples that always put a $(document).ready block around so that the example is self-contained. I wrote the comment for a future user who may, like me, stumble on this and end-up reading the comment thread and this beginner 'tip'

If you are applying this to your own code, note that the 'name' attributes are critical to the inputs otherwise serialize() will skip them.

javascript - jQuery Ajax POST example with PHP - Stack Overflow

php javascript jquery ajax post