How would you do a code-escape in PHP? - php

I have a huge list of stuff for a glossary ( about 17 pages worth ) that I have to put into an XML file. So I decided I'd use php to make it. My code works, except where ALL the XML code is, it doesn't show because it's trying to render it. Help?
$arg=explode("\n", $strang);
echo count($arg);
for ($i=0;$i<=count($arg);$i=$i+3)
{
echo "<word id='" . $arg[$i+1] . "'>";
echo "<desc>" . $arg[$i] . " - " . $arg[$i+2] . "</desc>";
echo "<pic></pic>";
echo "<audio></audio>";
}

I assume by render it you mean in your browser? If so, you'll need to escape the characters so they will be interpreted literally rather than as markup.
Check out htmlspecialchars and htmlentities

use CDATA construction:
echo "<desc><![CDATA[" . $arg[$i] . " - " . $arg[$i+2] . "]]></desc>";

If this is your entire script, fastest way would probably be to swap all of the <'s with <
$arg=explode("\n", $strang);
echo count($arg);
for ($i=0;$i<=count($arg);$i=$i+3)
{
echo "<word id='" . $arg[$i+1] . "'>";
echo "<desc>" . $arg[$i] . " - " . $arg[$i+2] . "</desc>";
echo "<pic></pic>";
echo "<audio></audio>";
}

Related

PHP won't echo html code to show a link on page

I have a simple echo statement that's supposed to display a link on the web page, but all it's doing is showing exactly "<a href=website.com>Link</a>" (without the quotes). To me, this should work with no problem. I thought maybe it's because the HTML for the website is in one file while the PHP is in another file.
foreach ($output as $output)
{
echo 'DATE: ' . $output['date'] . "\n";
echo 'TO: ' . $output['to'] . "\n";
echo 'FROM: ' . $output['from'] . "\n";
echo 'SUBJECT: ' . $output['subject'] . "\n";
echo "<a href=website.com>Link</a>\n\n";
}
Your loop isn't a valid one, you have $output as $output.
I also suggest printing $output before looping through it, to see if it even contains what you think it should.
In your 'a' tag, the actual URL needs to be in quotes
<a href='http://www.website.com'>

Send parameter to url in PHP

I'm having trouble sending a parameter into a url of another page. I'm new to coding in PHP so I do not really know how to get this through, already did some research on this about the $_GET method, but its still not working.
Code in 1st page:
echo "<tr><td><a href='application_desktop.php?id='". $temp ."'>" . $row['appl_nric_date'] . "</td><td>" . $row['applicant_name'] . "</td><td>" . $row['nric'] . "</td><td>" . $row['application_date'] . "</a></td></tr>";
where $temp is the parameter I want to pass to the url.
Code in 2nd page:
$id = $_GET['id'];
$applicants = mysql_query("SELECT * FROM tblapplication WHERE appl_nric_date = $id");
//$applicants = mysql_query("SELECT * FROM tblapplication WHERE appl_nric_date = 10");
The sql query returns error that the $id is null, and the url doesn't display the id.
Do it like this on your html line
echo "<tr><td>" . $row['appl_nric_date'] . "</td><td>" . $row['applicant_name'] . "</td><td>" . $row['nric'] . "</td><td>" . $row['application_date'] . "</td></tr>";
I ran your code to see what it output:
// Make some sample data
$row = [
'appl_nric_date' => '9999-99-99',
'applicant_name' => 'some-applicant',
'nric' => 'wtf-is-an-nric',
'application_date' => '8888-99-00'
];
$temp = 'something';
echo "<tr><td><a href='application_desktop.php?id='". $temp ."'>"
. $row['appl_nric_date']
. "</td><td>"
. $row['applicant_name']
. "</td><td>"
. $row['nric']
. "</td><td>"
. $row['application_date'] . "</a></td></tr>";
echo PHP_EOL;
This is what it outputs:
<tr><td><a href='application_desktop.php?id='something'>9999-99-99</td><td>some-applicant</td><td>wtf-is-an-nric</td><td>8888-99-00</a></td></tr>
The use of single quotes is not right. Remove the single quote after id=.
Looks like you are not nesting your html elements correctly. You place the opening A tag inside the first TD but then you close that TD without closing the A tag.
In order to debug what you are doing this in the browser, then the address bar where the url lives should show the parameters that are sent to the destination page. You can just look at that to verify that it sent what you intended.
In the destination page, you can add the following to debug:
<pre>
<?php print_r($_GET) ?>
</pre>
The above will let you see what you are getting from the first page.

Syntax Error on php echo

I am having trouble getting the URL to display correctly. This works now, but the link has " in front of the url. and yet the url link works fine
echo "<br>" . $result['text'] . " <em>(Contributed by: \"<a
href=\"http://example.com/portfolio?ID=$result[ID]\">" .
$result['display_name'] . "</a>" . ")</em>" . "<br>";
sigh:
echo '<br>' . $result['text'] . ' <em>(Contributed by: <a
href="http://example.come/portfolio?ID="'.$result['ID'].'">'.
$result['display_name'] .'</a>)</em><br>';

php in header url error

I have just a quick question. i was using a normal html link tag to redirect to a paypal checkout page and it was working fine even when i had php inside the url. but when i was using it in a php header
the url cuts off where i enter the php.
header('location: https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=XXXX&lc=UK&item_name=<? echo $product . " " . $server ?>&amount=<? echo $xprice1; ?>%2e00&currency_code=GBP&button_subtype=services&no_note=0&bn=PP%2dBuyNowBF%3abtn_buynowCC_LG%2egif%3aNonHostedGuest');
You are placing the PHP code inside of the location redirect as a string. The code is not being evaluated as PHP.
Try this instead:
<?php
$url = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=XXXX&lc=UK&item_name=" . $product . " " . $server ."&amount=" . $xprice1 . "%2e00&currency_code=GBP&button_subtype=services&no_note=0&bn=PP%2dBuyNowBF%3abtn_buynowCC_LG%2egif%3aNonHostedGuest";
header('location: ' . $url);
Or, you could keep it in one line like so:
<?php
header('location: ' . "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=XXXX&lc=UK&item_name=" . $product . " " . $server ."&amount=" . $xprice1 . "%2e00&currency_code=GBP&button_subtype=services&no_note=0&bn=PP%2dBuyNowBF%3abtn_buynowCC_LG%2egif%3aNonHostedGuest");
header('location: https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=XXXX&lc=UK&item_name=<? echo $product' . " " . '$server ?>&amount=<? echo $xprice1; ?>%2e00&currency_code=GBP&button_subtype=services&no_note=0&bn=PP%2dBuyNowBF%3abtn_buynowCC_LG%2egif%3aNonHostedGuest');
This may solve your problem. Please dont forget to tick the answer right if it works.

correct way to echo a link with a onclick javascript function

my question is how I can echo this the right way
because the variable in the onclick function gives out a undefined error
$openchat="<a href='javascript:void(0)' onClick='return chatWith(" . $livenaam .")'>" . $livenaam . "</a><br>";
echo $openchat;
I want to use it in a loop to get a list off users online for the chat
Thanks, Richard
Looks like you are missing some quotes:
$openchat="<a href='javascript:void(0)' onClick='return chatWith(\"" . $livenaam ."\")'>" . $livenaam . "</a><br>";
or for increased security:
$openchat="<a href='javascript:void(0)' onClick='return chatWith(\"" . htmlspecialchars($livenaam,ENT_QUOTES) ."\")'>" . htmlspecialchars($livenaam,ENT_QUOTES) . "</a><br>";
Try this:
'' . htmlspecialchars($livenaam) . '<br>'
If json_encode is not available, try this:
'' . htmlspecialchars($livenaam) . '<br>'

Categories