Am passing a value using href tag
In first page Href tag used as
echo "$compname";
In the Second page used
$compname = $_GET['compna'];
To receive the Compna values are pass but only the first word is passed remaining words are skipped.
Compname as " Chiti Technologies Ltd "
When I pass the value I receive onlt "Chiti"
The reason you're only getting the first word of the company name is that the company name contains blanks. You need to encode the name.
echo "$compname";
You are producing ambiguous/invalid HTML by not quoting the parameter. The result is something like:
<a href=foo bar baz>
Only foo is recognized to belong to href, the rest doesn't. Quote the values:
echo '', htmlspecialchars($compname), '';
Use this code:
echo ''.$compname.'';
You need add quotes for your href, besides, you also need to use urlencode to encode the variable.
echo '' . $compname . '';
change echo "$compname";
to echo "$compname";
When using double quoted strings " you don't need to paste variables in between, you can just type them.
Also, when pasting strings together don't use comma's , but use . to paste strings otherwise you'll get parse errors.
For arrays include them between curly brackets {}
echo "$compname";
Related
I am trying to display a URL stored in mysql as a link in php table like this
echo "<td>Resume</td>";
where $row['resume'] retrieves correct data using mysql_fetch_array
However the whitespace between file link gets truncated automatically
for example my file name is "this is a resume.doc" i only get "this" in link
help.
You need to place quotes around your href attribute.
echo "<td>Resume</td>";
You need to do several things:
Escape characters with special meaning in URLs using urlencode
Escape characters with special meaning in HTML using htmlspecialchars
Quote attribute values
Such:
$url = htmlspecialchars( urlencode( $row['resume'] ) );
echo "<td><a href='$url'>Resume</a></td>";
I have a print statement in some PHP code:
print "<a href='item.php?id='{$row[0]}''><img src='{$row[0]}.jpg'></a>";
In {$row[0]} is a int. When I click on the image I get to a page "restofweburl/item.php?id=" with no number at the end of the URL. What am I doing wrong?
You end the href prematurely with a single quote, after id=. Change the line to:
print "<a href='item.php?id={$row[0]}'><img src='{$row[0]}.jpg'></a>";
This is because after the href you have two single quotes. I prefer to use the following syntax:
print '<img src="'.$row[0].'".jpg"/>';
I used single quotes for the print so I can use double quotes for the html attributes (alt/src). This is a prefered method, not a rule, but quite common practice.
As you can see, the color highlighing shows you where the echo ends, a variable gets inserted and the string continues. This makes it easier to spot small mistakes like your double single quote problem.
I have a hint echo'd however, i have a issue with " and ' i can echo numerical values to the string, but not words..
$hint='<a href="javascript:void(0)"
onclick="javascript:document.contactForm.musicDetailTitle4.value=5;
document.contactForm.musicDetailArtist4.value=foo;">fill form</a>'.
5 works but foo doesn't works.
UPDATE
Still not getting an output
$hint='fill form'.
Whole Code
echo $hint='fill form'.$artist."-".$title."-".$id."</a>";
Output is...
fill formTomato Soup-Heinz-0001fill formTomato Soup-Heinz-0001
You need to escape the quotes
$hint='fill form'.
It doesn't have much to do with PHP but rather JavaScript.
When passing a numeric value you just pass the number itself, but when passing strings you must wrap them in quotations otherwise the compiler will mistake "foo" for a variable named foo which may or may not exist.
As others mentioned, all you have to do is wrap your string like so:
\'foo\'
The slashes are because you don't want to close your echo which was also opened using a single quote, so you need to escape the character so when it's echoed to the user it will become 'foo'.
Try this -
$hint='fill form'.
When declaring a string value you must add quotes, and when adding it in this way you must escape those quotes using the \ key.
I am trying to display a URL stored in mysql as a link in php table like this
echo "<td>Resume</td>";
where $row['resume'] retrieves correct data using mysql_fetch_array
However the whitespace between file link gets truncated automatically
for example my file name is "this is a resume.doc" i only get "this" in link
help.
You need to place quotes around your href attribute.
echo "<td>Resume</td>";
You need to do several things:
Escape characters with special meaning in URLs using urlencode
Escape characters with special meaning in HTML using htmlspecialchars
Quote attribute values
Such:
$url = htmlspecialchars( urlencode( $row['resume'] ) );
echo "<td><a href='$url'>Resume</a></td>";
Look at the code below
echo "$_SERVER['HTTP_HOST']";
it show 'Parse error', while the next shows ok
$str = $_SERVER['HTTP_HOST'];
echo "$str";
That was very strange to me.
to refer an associative array inside of a string, you have to either add curly braces or remove quotes:
both
echo "{$_SERVER['HTTP_HOST']}";
echo "$_SERVER[HTTP_HOST]";
would work
http://php.net/manual/en/language.types.string.php <- extremely useful reading
in this case, you should use bracket to point out the variable range in string
echo "{$_SERVER['HTTP_HOST']}";
see also
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double
As you have discovered, referring to variables inside a string literal is error-prone and should be avoided. Separate string literals from variables, and use single quotes whenever possible:
echo 'Host: header: ' . $_SERVER['HTTP_HOST'] . "\n";
If you really want to use complex variable expressions inside string literals (and you shouldn't!), remove the inner quotes:
echo "Host header: $_SERVER[HTTP_HOST]";
or surround the variable reference inside curly braces:
echo "Host header: {$_SERVER['HTTP_HOST']}";
If a variable is detected in a double-quoted string, it will automaticly be converted to it's string value. But that's not every your case. the fact that the key of your array is also a string comes in conflict with the php parser. ex :
$ar = array("key" => "val");
echo "$ar['key']"; // won't work
echo "$ar[0]"; // will work because the key is not a string
Anyway, like others already said, the best solution is to encapsulate your variable in curly braces :
echo "{$ar['key']}";
it should be
echo $_SERVER['HTTP_HOST'];
if you want to output $_SERVER['HTTP_HOST'], escape that dollar sign
echo "\$_SERVER['HTTP_HOST']";
or put it into single quote
echo '$_SERVER["HTTP_HOST"]';
Clarification: You should not put only variables into quotes if you want content of actual variable, just delete these quotes and it will work