DiscreteTomatoes
DiscreteTomatoes

Reputation: 789

syntax error when creating html code with php echo

hello and thank you for reviewing my question, the code below demonstrates an echo command printing html code - the problem i'm having is that the form action does not call the same pages as it should, Instead i'm getting an error with my webhost. Can someone correct my syntax? (as you can see ive tried to escape all the quotes which works except for the action attribute - i only get an error when the form attempts to execute the action).

echo "
<form name=\"CartUpdateForm\" id=\"CartUpdateForm\" method=\"post\" action=\"<?php echo htmlspecialchars(\$_SERVER[\"PHP_SELF\"]); ?>\">
 <input name=\"SCUpdateBut\" id=\"SCUpdateBut\" type=\"submit\" value=\"Update\">
 <input name=\"SCRemoveBut\" id=\"SCRemoveBut\" type=\"submit\" value=\"Remove\">
</form> ";

Upvotes: 0

Views: 314

Answers (3)

Vadivel S
Vadivel S

Reputation: 660

You Try this

  echo "
  <form name='CartUpdateForm' id='CartUpdateForm' method='post'  action=' ".htmlspecialchars($_SERVER['PHP_SELF'])." '>
  <input name='SCUpdateBut' id='SCUpdateBut' type='submit' value='Update'>
  <input name='SCRemoveBut' id='SCRemoveBut' type='submit' value='Remove'>
  </form> ";

Upvotes: 1

Panda
Panda

Reputation: 6896

You cannot have PHP tags within PHP.

Thus, you can echo the first part, join htmlspecialchars($_SERVER["PHP_SELF"]), then echo the last part, in PHP, . string concatenation operator.

echo'
<form name="CartUpdateForm" id="CartUpdateForm" method="post" action="' . htmlspecialchars($_SERVER["PHP_SELF"]) . '">
<input name="SCUpdateBut" id="SCUpdateBut" type="submit" value="Update">
<input name="SCRemoveBut" id="SCRemoveBut" type="submit" value="Remove">
</form>';

Hope this helps!

Upvotes: 1

Kausha Mehta
Kausha Mehta

Reputation: 2928

First why are you using echo for displaying a form, where you need to use PHP variable or value, write tag before it. It is good Practice.

echo '
<form name="CartUpdateForm" id="CartUpdateForm" method="post" action="'.htmlspecialchars($_SERVER["PHP_SELF"]).'">
 <input name="SCUpdateBut" id="SCUpdateBut" type="submit" value="Update">
 <input name="SCRemoveBut" id="SCRemoveBut" type="submit" value="Remove">
</form>';

Upvotes: 1

Related Questions