How to pass php variable to html? ex. $tmp='<a href="#" onClick="addtext($var)">' . $tmp . '</a>'?
-
$var contains mysql data and onClick on $tmp should popup box with $var data. how to popup box $var onClick on $tmp ????????
-
Answer:
$tmp='<a href="#" onClick="addtext($var)">' . $tmp . '</a>'; is fine if you have a javascript function named addtext(). but may I suggest you surround $var with quotes? also, you are using the wrong kind of quotes in PHP for variable substitution, should be using double-quotes (") instead. $tmp="<a href=\"#\" onClick=\"addtext('$var')\">$tmp</a>"; or $tmp='<a href="#" onClick="addtext(\'$var\')">'.$tmp.'</a>… you can escape a " or ' with a \ character. when you do, it will simply embed the quote character in the string. I don't know much about how to do popup boxes these days, with popup killers and all. so I just avoid using them. actually, you CAN pass data from PHP to HTML via javascript through hidden form tags, that is a one-way transmission of data. javascript *might* ba able to modify those hidden form tags' values. what you are talking about can also be accomplished through AJAX which is the term for what you are describing, where the client and the server pass data back and forth to each other, but usually via XML.
Panche at Yahoo! Answers Visit the source
Other answers
As mentioned, JavaScript is going to be the key for you to bring the variables in. PHP is working on the server. It does the easy job (because your sever is a known entity.) Then it closes its eyes and passes the html page on to the browser and it no longer has anything to do with what goes on on the page. Here the hard working scripting language comes in. JavaScript has to be able to deal with different browser, different screen sizes, and in some case, different user settings as to what it can and cannot do. As mentioned by another answerer, pop-ups are often blocked. You can give two options, a pop up and adding the value to the page, all through JavaScript. As far as all the quotes and stuff, the more I have used PHP (and I am just finishing putting together a blog...lots of PHP)...the more stuff you cram into one line, the more confused you get. So, I can't say it is the best way, but to me I found it kept it cleaner -> I created lots of code blocks within the html bits. Otherwise I cut things up into peaces and created strings of variables, something like this: $sliceA = "<a href='"; $sliceB = $url_variable; $sliceC = "' title='"; $sliceD = $name; and so on. It looks confusing, but makes it possible to string things together in a variety of ways. I have played around with what you were asking, and there is a little code below to add to what was given already. I used buttons instead of links, but they both allow similar methods, so it's neither here nor there. I have made the variable pop up and also display in a standard set of span tags. This way, if the user's browser does block the pop up, it is still displayed. As mentioned already, don't over do the pop ups. I try only to use them to warn things like empty form fields. <html><head><title>Pop Up PHP values</title> <style type="text/css"> body{margin:0;padding:0;border:0;backg… .inner{margin:0;padding:20px;} #container{margin:0;padding:0;display:… #block1{display:block;float:left;margi… #block2{display:block;float:left;margi… #block3{display:block;float:left;margi… ul{list-style:none;} input[type=button]{background-color:#c… dashed white;cursor:pointer;margin:10px 0 0 0;padding:5px;width:150px;} </style> <script type="text/javascript"> function addtext(xVar){ alert(xVar); document.getElementById('getit').inner… = xVar; } </script> <?php /* $var1 - $var5 to come from database */ $var1 = "I am in favor of variable number one"; $var2 = "I am in favor of variable number two"; $var3 = "I am in favor of variable number three"; $var4 = "I am in favor of variable number four"; $var5 = "What are variables and can you eat them?"; $tmp1 = "one"; $tmp2 = "two"; $tmp3 = "three"; $tmp4 = "four"; $tmp5 = "Wildcard"; ?> </head><body><div id="container"> <div id='block1'><div class='inner'> <ul> <li><input type='button' onclick="addtext('<?php echo $var1; ?>')" value="<?php echo $tmp1; ?>" /></li> <li><input type='button' onclick="addtext('<?php echo $var2; ?>')" value="<?php echo $tmp2; ?>" /></li> <li><input type='button' onclick="addtext('<?php echo $var3; ?>')" value="<?php echo $tmp3; ?>" /></li> <li><input type='button' onclick="addtext('<?php echo $var4; ?>')" value="<?php echo $tmp4; ?>" /></li> <li><input type='button' onclick="addtext('<?php echo $var5; ?>')" value="<?php echo $tmp5; ?>" /></li> </ul> </div></div> <div id='block2'><div class='inner'><h1>Which variable is the best?</h1><p><span id="getit"></span></p></div></div> </div></body></html>
Namibnat
you can't "pass" anything to HTML. HTML is a static language. You can $tmp="<a href=\"#\" onClick=\"addtext($var)\">\" . $tmp . \"</a>"; echo $tmp;
DzSoundNirvana
you could use the javascript alert() ie. <?php $var = "some text"; echo "<a href="#" onclick='alert($var);'>html anchor</a>"; ?>
ecofer
Related Q & A:
- How to pass javascript jQuery variable value in php array?Best solution by Stack Overflow
- How to pass the SelectedValue in html.Dropdownlist MVC?Best solution by codeproject.com
- How do I display limited html content in a webview?Best solution by Stack Overflow
- How to pass a variable from AlertDialog to an Activity?Best solution by Stack Overflow
- What's the HTML code for a scroll box on top of a picture?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.