tags:

views:

93

answers:

3

I am facing a problem in my code. Whenever I try to insert a text field then it's giving an error. What's wrong in the syntax here?

   print '<table>';
  print "<tr style='background-color:#CDC9C9;'>
<td><A HREF=\"http://localhost/cgi-bin/AddUser.cgi\"&gt;ADD&lt;/A&gt;&lt;/td&gt;
<td></td>
<td><b>UserId</b></td>
<td><input type="text" name="UserId"></td>
<td><b>UserName</b></td>
 <td><input type="text" name="User_Name"></td>
 <td><input type="submit" name="Filter" value="Filter">  </td>
  </tr>";
print"</table>"; 
+4  A: 

You need to escape quite a few "

Try:

print '<table>';
print "<tr style='background-color:#CDC9C9;'>
<td><A HREF=\"http://localhost/cgi-bin/AddUser.cgi\"&gt;ADD&lt;/A&gt;&lt;/td&gt;
<td></td>
<td><b>UserId</b></td>
<td><input type=\"text\" name=\"UserId\"></td>
<td><b>UserName</b></td>
<td><input type=\"text\" name=\"User_Name\"></td>
<td><input type=\"submit\" name=\"Filter\" value=\"Filter\">  </td>
</tr>";
print"</table>";

A better alternative would be to use a heredoc as:

$table = << "TABLE";
<table>
<tr style='background-color:#CDC9C9;'>
<td><A HREF="http://localhost/cgi-bin/AddUser.cgi"&gt;ADD&lt;/A&gt;&lt;/td&gt;
<td></td>
<td><b>UserId</b></td>
<td><input type="text" name="UserId"></td>
<td><b>UserName</b></td>
<td><input type="text" name="User_Name"></td>
<td><input type="submit" name="Filter" value="Filter">  </td>
</tr>
</table>
TABLE
print $table;
codaddict
A: 

You need to escape the double quotes using "\". For bigger strings I suggest you use a HEREDOC.

e.g.:

print "My string contains quite some \"double quotes\"";
halfdan
Use alternate quoting instead. :)
brian d foy
oh ok Thanks for the information Mr.halfdan...
sonya
+5  A: 

If you have a double-quoted string then it can't contain unescaped double-quotes (for, hopefully, obvious reasons).

Some ways to get around it:

1/ Escape the double quotes.

print "<tr style='background-color:#CDC9C9;'>
<td><A HREF=\"http://localhost/cgi-bin/AddUser.cgi\"&gt;ADD&lt;/A&gt;&lt;/td&gt;
<td></td>
<td><b>UserId</b></td>
<td><input type=\"text\" name=\"UserId\"></td>
<td><b>UserName</b></td>
<td><input type=\"text\" name=\"User_Name\"></td>
<td><input type=\"submit\" name=\"Filter\" value=\"Filter\">  </td>
</tr>";

2/ Switch to a single-quoted string (as your string contains no variables or escape sequences).

print '<tr style="background-color:#CDC9C9;">
<td><A HREF="http://localhost/cgi-bin/AddUser.cgi"&gt;ADD&lt;/A&gt;&lt;/td&gt;
<td></td>
<td><b>UserId</b></td>
<td><input type="text" name="UserId"></td>
<td><b>UserName</b></td>
<td><input type="text" name="User_Name"></td>
<td><input type="submit" name="Filter" value="Filter">  </td>
</tr>';

Note: I had to change the single quotes in the style attribute to double quotes here.

3/ Use a here-doc.

print <<END_OF_HTML;
<tr style='background-color:#CDC9C9;'>
<td><A HREF="http://localhost/cgi-bin/AddUser.cgi"&gt;ADD&lt;/A&gt;&lt;/td&gt;
<td></td>
<td><b>UserId</b></td>
<td><input type="text" name="UserId"></td>
<td><b>UserName</b></td>
<td><input type="text" name="User_Name"></td>
<td><input type="submit" name="Filter" value="Filter">  </td>
</tr>
END_OF_HTML

4/ Choose a different quoting character.

print qq[<tr style='background-color:#CDC9C9;'>
<td><A HREF="http://localhost/cgi-bin/AddUser.cgi"&gt;ADD&lt;/A&gt;&lt;/td&gt;
<td></td>
<td><b>UserId</b></td>
<td><input type="text" name="UserId"></td>
<td><b>UserName</b></td>
<td><input type="text" name="User_Name"></td>
<td><input type="submit" name="Filter" value="Filter">  </td>
</tr>];

But like so many of your problems, the real solution is to use a templating system.

davorg
A milion Thanks to you Mr. Davorg my code is working the way i wanted it to...thanks alot..
sonya
If my answer is so useful to you, then why haven't you voted it up?
davorg
am sorry sir i did not knew that..am new to the forum so i could not recognize it...i have marked it now..
sonya
No. You haven't voted it up, you've accepted it (and, in the process, unaccepted codaddict's answer which you had previously accepted). At to top left of each answer on Stack Overflow you'll see the number of votes the answer has gained and up and down arrows. You click on the up arrow to vote up an answer you like and click on the down arrow to vote down an answer you don't like.
davorg
when i clicked the vote up arrow i got a message saying "voting requires atleast 15 reputations"...No idea what is this reputation is al about sir..
sonya
Ah. I'd forgotten that people with low reputations can't vote. Your reputation is currently 6. To raise it, you need to post questions than answers that people will vote up.
davorg
@jene: just a warning -- if you don't start phrasing your questions more clearly (and have your questions *up*voted rather than down), you won't be able to post any more questions at all. [This link](http://meta.stackoverflow.com/questions/56817/can-we-prevent-some-of-the-low-quality-questions-from-entering-our-system/60294#60294) describes the details.
Ether