php inserting link parts into http - php

I am trying to retrieve parts of the getcwd() method and inserting them into window.open()
the current getcwd() gives me this C:\wamp\www\qa4u\qa4u_working\Presenter
Using this code :
<?php
if(isset($_POST['genPDF'])){
foreach($_POST['email'] as $email)
{
$eid=$_POST['eid'];
?>
<script type="text/javascript" language="Javascript">
<?
$stringlink = getcwd();
$pieces = explode('\\', $stringlink);
?>
window.open("http://"+"<?php echo $_SERVER['HTTP_HOST']?>"+"/"+"<?php $pieces[3]?>"+"/"+"<?php $pieces[4]?>"+"/"+"<?php $pieces[5]?>"+"/genPDF.php?eid=<?php echo $eid ?>&email=<?php echo $email ?>");
</script>
<?php
}
}
?>
I am trying to achieve this instead :
window.open("http://qna.nyp.edu.sg/qa4u/qa4u_working/presenter/genPDF.php?eid=<?php echo $eid ?>&email=<?php echo $email ?>");
without forming static links is there a way to get the code to work ?

2 issues regarding your code, that I believe prevents it from working.
You're not printing the pieces values. You should use echo, print or a shorthand <?=$var ?>
Since those are PHP variables, you don't need to use the JS + symbol
So, you should update your code:
window.open("http://<?php echo $_SERVER['HTTP_HOST']; ?>/<?php echo $pieces[3]; ?>/<?php echo $pieces[4]; ?>/<?php echo $pieces[5]; ?>/genPDF.php?eid=<?php echo $eid; ?>&email=<?php echo $email; ?>");

Related

PHP echo to display image HTML

I am trying to display an image on my webpage using a PHP script to determine which image is displayed.
The image link is as follows:
......
My PHP script is thus:
<?php
$result = $_GET['image'];
echo '<img src="images/gallery/'.$result.'.jpg">';
?>
So what I am trying to achieve in terms of HTML is:
<img src="images/gallery/image01.jpg">
The result I am getting is '"; ?>' displayed on the page.
Any help would be much appreciated!
You have to change your code like this
<?php
$result = $_GET['image'];
?>
<img src="images/gallery/<?php echo $result; ?>.jpg">
<?php
$result = filter_input ( INPUT_GET , 'image' );
if (isset($result) && !empty($result)) {
echo '<img src="images/gallery/'.$result.'.jpg">';
}
?>
You used echo wrong, here is how you should use it.
<?php
$result = $_GET['image'];
?>
<img src="images/gallery/<?php echo $result ?>.jpg">
I would change the gallery.php to this:
<?php $result = $_GET['image']; ?>
<img src="images/gallery/<?php echo $result; ?>.jpg">
That would simply it a little bit. You should echo out the result to see what you are getting when the variable is passed to the gallery page.
echo"<img src='{$image}'>";
$image = uploads/myImage.jpg
I think this is the simplest code. To use a php variable while echoing out html, use curly {} brackets to insert any php variable. For instance, a file upload...
<?php
if(isset($_POST['submit'])){
$filename=$_FILES['file']['name'];
$temp_dir=$_FILES['file']['tmp_name'];
$image = "img/".$filename;
}
?>
<?php if($row2['pack1']==1){ echo "<img src=".BASE_URL."images/1seo.png"; } ?>

Rendering a Global or a Var in a Php echo

I found a good script anti spam robots. To implement it in my dynamic pages I have to paste this code:
<?php echo hide_email('test#test.com'); ?>
Since my pages are dynamic, my emails are stored in Globals
%%GLOBAL_Email%%
<?php echo hide_email('%%GLOBAL_Email%%'); ?> <--- The email is not rendered
Even if I transform the Global in a var it does not work, I tryed the following:
<?php echo hide_email(' . $email . '); ?>
<?php echo hide_email('" . $email . "'); ?>
Answer:
<?php echo hide_email($email); ?>
The question was simple, the answer too.
Here is the answer:
<?php echo hide_email($email); ?>

Php showing pictures using array

I'm trying to show images from some directory using foreach.. But the problem is it's showing results in array, so if i want to print out first image I have to use $imag['0']..
Is there any way that I can bypass this number in this brackets?
Here's my code...
<?php
$domena = $_SERVER['HTTP_HOST'];
$galerija = $_POST['naziv'];
$galerija = mysql_real_escape_string($galerija);
define('IMAGEPATH', 'galleries/'.$galerija.'/');
foreach(glob(IMAGEPATH.'*') as $filename){
$imag[] = basename($filename);
?>
<img src="http://<?php echo $domena; ?>/galerija/galleries/<?php echo $galerija; ?>/<?php echo $imag['0']; ?>">
If you only need the first filename, then you could avoid the loop and directly access the first element of the array and use it afterwards:
$files = glob(IMAGEPATH.'*');
$filename = array_shift(array_values($files));
$image = basename($filename);
And to display it, you could use sprintf():
echo sprintf('<img src="http://%s/galerija/galleries/%s/%s"/>',
$domena, $galerija, $image);
Well you could first not create the array in the foreach statement and instead just print the img:
echo '<img src="', $domena ,'/galerija/galleries/', $galerija ,'/', $filename,'">';
Or you could iterate the array.
foreach($imag as $img): ?>
<img src="http://<?php echo $domena; ?>/galerija/galleries/<?php echo $galerija; ?>/<?php echo $img ?>">
<?php endforeach; ?>

How to add link within if-statement?

I have some problems to put a URL-function within an if-statement properly. The following method works fine outside of my if-statement and the newly created link refers to this URL: "h**p://www.blog.com/?location=Bern&date=1"
<?php $rasp = $_GET["location"]; ?>
<a href="<?php echo preg_replace("/&date=(0|1|2|3)/", "", $_SERVER[\REQUEST_URI\])."&date=0"; ?> " >Heutel</a>
If-Statement works also just fine when I put simply an URL (e.g. https://www.google.com/) in between the quotation marks, then if I put the URL-function above in there, the newly created link refers to this: "h**p://www.blog.com/%3C?php%20echo%20preg_replace%28". Actually it should refer to the URL above "h**p://www.blog.com/?location=Bern&date=1"
URL-Function within if-statement:
<?php $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (false !== strpos($url,'date=1')) { echo
'<?php $rasp = $_GET["location"]; ?>
<a href="<?php echo preg_replace("/&date=(0|1|2|3)/", "", $_SERVER[\REQUEST_URI\])."&date=0"; ?> " >Heutel</a> ' ;
} else {
echo 'No cars.';
} ?>
Any ideas?
Solution thanks to IMSop:
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (false !== strpos($url,'date=1')) {
?>
<a href="<?php echo preg_replace("/&date=(0|1|2|3)/", "", $_SERVER['REQUEST_URI'])."&date=0"; ?> " >Heutel</a>
<?php
}
else {
echo 'No cars.';
}
This sequence makes no sense:
echo '<a href="<?php echo ...
A quoted string and direct output outside the <?php ... ?> markers are completely different things. The ' starts a string, which will continue until you put another '; the <?php would start a block of PHP code if you weren't in one, but you already are - if you weren't the echo wouldn't mean anything.
To join multiple strings together, you can use the . ("concatenation") operator:
echo '>Heutel';
Alternatively, drop out of PHP mode like you were before, but with the if in place:
<?php
// In PHP mode...
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (false !== strpos($url,'date=1')) {
// Now leaving PHP mode, but still inside the if condition...
?>
<a href="<?php echo preg_replace("/&date=(0|1|2|3)/", "", $_SERVER['REQUEST_URI'])."&date=0"; ?> " >Heutel</a>
<?php
// Re-enter PHP mode to close off the if statement
}
There's even an alternative syntax for control syntax which some people prefer to use in cases like this:
<?php
if ($some_condition) :
?>
your output here
<?php
endif;
?>
which is exactly the same as
<?php
if ($some_condition) {
?>
your output here
<?php
}
?>

very simple javascript failing

Working Example:
This is almost identical to code I use in another places on my page but fails here for some reason.
<?php
//$p = "test";
?>
<script>
alert('posts are firing? ');
parent.document.getElementById('posts').innerHTML = "test";
</script>
Failing example: (alert still works)
<?php
$p = "test of the var";
?>
<script>
alert('posts are firing? ');
parent.document.getElementById('posts').innerHTML = '<?php $p; ?>';
</script>
Try
'<?php echo $p; ?>';
or
'<?= $p ?>';
Debugging 101: Start checking all variable values.
alert(parent);
alert(parent.document);
alert(parent.document.getElementById('posts'));
as well as the value rendered by: '<?php $p; ?>'
Make sure your 'posts' object (I guess it is DIV or SPAN) loads before you fill it using javascript.
You're trying to generate javascript with php, here I use a simple echo:
<?php
$p = "test of the var";
echo"
<div id='posts'></div>
<script type='text/javascript'>
var posts = document.getElementById('posts');
posts.innerHTML = '$p';
</script>
";
?>
Note the $p and that the div is printed before the javascript!
You are not outputting the variable data is why it isn't working. You need to echo or print the variable $p.
In your example the $p is being evaluated, not printed.
To print it you should use print, echo, or the syntax <\?=$p;?>. without the \

Categories