Pass a PHP variable in a link to a modal window - php

Hi I have a php link which calls a modal popup window which all works fine, the problem i am having is that it doesn't pass the php variable in the link.
the php variable shows in the address bar, but it isn't passing to the modal form.
here is my link
echo "<a href=\"?ip_address='.$ip_address.'#accSettings1\" >Add</a>";
And I am getting the variable like this.
$ip_address = $_GET['ip_address'];
echo "$ip_address";

You need double quotes and not single quotes
<? echo "<a href=\"?ip_address=".$ip_address."#accSettings1\" >Add</a>"; ?>
-^-------------^-
It will pass . and ' as literal string, the output will be something like
Assuming $ip_address = 'hello';
<a href="?ip_address='.hello.'#accSettings1" >Add</a> <!-- HTML Source -->
-^-------^-
This is why the link breaks
so it is adding unnecessary periods and single quotes in the link which breaks it
And the code I provided will be
<a href="?ip_address=hello#accSettings1" >Add</a>

Related

How to put html href button inside php code

I'm trying to put an html href button inside of my php code, but when I run it, I get an error message saying "This Page Isn't Working."
<?php
echo "<button onclick="location.href='phpfile.php';">My Button</button>";
?>
Either you need to escape the HTML double quotes:
echo "<button onclick=\"location.href='phpfile.php';\">My Button</button>";
Or the Javascript single quotes:
echo '<button onclick="location.href=\'phpfile.php\';">My Button</button>';
By the way,
This Page Isn't Working.
Means a 500 error, which means your php script does not compile. Most of the time it is due to syntax errors.

PHP Pass variable through URL using ?object=value

I know there's a bunch of these topics out there, but all the answers are simply not working for me. I've tried everything I could find, and I'm still right where I started. I'm trying to pass a variable through a URL:
link
When clicking on the "link", it redirects me to the appropriate page (test2.php), but leaves the value blank (resulting in localhost/test2.php?one=). This means I can no longer see what variable was being sent.
$one = $_GET['one'];
echo $one;
How can I properly send the variable from test.php to test2.php?
Remove quotes from the href value:
link
<!-- ^^^ - quotes missing here -->
you closed the href try this :
link

Passing variable to the new popup window

I have a table in which there are small href links assigned in each row which i have set using below code working fine:
<h10>(check status)</h10>
Now I do not want to display the same above href link as static but want to send my variable value to the pop window and need to get data displayed separately for each row.
In short i want to pass my variable $name in the popup window. I am trying below code but but isn't happening. Even i tried <FORM> by GET
<?php echo "<h10>(check status)</h10>";?>
On popup.php page:
<?php echo $id = $_GET["id"]; ?>
What i want want is to pass variable using any method (GET or HREF), and it should open in new popup window not in next tab or forwarded window
change this
<?php echo "<h10>(check status)</h10>";?>
to
<?php echo "<a target='_blank' href='popup.php?id=".$name."' onclick=\"window.open(this.href, 'popupwindow', width=400,height=300,scrollbars,resizable');return false;\"><h10>(check status)</h10></a>";?>
^ ^ ^ ^// here was the mistake
You have not concatenated the strings properly and you missed ' for herf attribute.
Use target="_blank" for opening link in new tab or new browser.

Passing HTML to javascript function

I have a variable in which a complete html is saved.
$body= $myhtmlpage;
<a onclick="openWin(' <?php echo htmlspecialchars(json_encode($body)) ?>');" href="javascript:void(0);"> Click </a>
and i have this javascript function which display the text in new window.
<script type="text/javascript">
function openWin( str )
{
myWindow=window.open('','','width=400,height=400');
myWindow.document.write(str+"<p>This is 'myWindow'</p>");
myWindow.focus();
}
</script>
When there is simple text in my body, it works fine. but if there is some html then it does not display, I am new to javascript. please tell me how can i prepare my HTML that it should be passed to Javascript html. i tried htmlspecialchars(json_encode($body))
functions but still having problem.
Uncaught SyntaxError: Unexpected identifier
You will have a long battle trying to get a lot of HTML to work as a string variable in Javascript. It would be far better for you to put that markup in a hidden block (like a DIV) in your markup and then just get the contents of that markup and show it in your window.
This has the added advantage of allowing your hidden markup to be validated. It is very hard to debug a lot of html markup stuffed into a string variable, but when included in the DOM as real markup it makes your life much easier.
UPDATE: Adding some sample code:
<div id="my_hidden_content" style="display:none;">
<?php echo $body; ?>
</div>
<a onclick="openWin('my_hidden_content');" href="javascript:void(0);"> Click </a>
Now the javascript:
function openWin( contentId )
{
var contentContainer = document.getElementById(contentId);
var content = contentContainer.innerHTML;
myWindow=window.open('','','width=400,height=400');
myWindow.document.write(content+"<p>This is 'myWindow'</p>");
myWindow.focus();
}
Firstly, you do not need to use json_encode(), that is just confusing the situation.
Secondly, the problem will be that your HTML contains quotes. This will result in a syntax error in the HTML you output, since htmlspecialchars() does not escape quotes.
Use htmlentities() with the ENT_QUOTES flag instead. So change the line to this:
<a onclick="openWin('<?php echo htmlentities($body, ENT_QUOTES) ?>');" href="javascript:void(0);">Click</a>
Thirdly (although it should probably be firstly since it is the most important point) your approach to this is all wrong. If you're opening a new window, you should have it load a page from the server and generate the HTML when the window is opened.
You can try html code inside php code, instead php code inside html.
for e.g.
<?php
echo "<a onclick='openWin(\" ".htmlspecialchars(json_encode($body))." \")'>Click</a>";
?>
That's because, HTML isn't JSON. For that simply use: htmlspecialchars($body)

popup window not work in php echo

it can appear a simple question but i have searched untill writing here but no answer. i have a php code and i what to start a pop up window after echo :
echo "<A HREF='map2.php' onClick='return popup(this,'notes')'>WHATEVER</A>";
in the head section i have :
<SCRIPT TYPE="text/javascript">
<!--
function popup(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
href=mylink;
else
href=mylink.href;
window.open(href, windowname, 'width=400,height=235,scrollbars=yes');
return false;
}
at the end is the ending script tag but i dont succide in adding it.
anyway.the pop up doesn work. the link opens in the same page.
i also tried :
About
and it doesnt work. it opens in the same page. The funny thing is that all these 2 solutions worked in html page, but when used between php , after "echo" , it doesnt work anymore.
In the first line you posted (the php echo), it seems to me you have a problem with ' in side '
Try the following:
echo "WHATEVER";
The issue here your quoting.
When outputting HTML I recommend using single quotes with echo as it allows you to use the proper double quotes for the HTML tags.
echo 'Whatever';
The problem with your original code was that you had quotes within quotes that were breaking the syntax. Read the link I posted to see how to handle quotes properly with PHP.

Categories