Never mind, guys. Thanks for your input. I had another bit of JS grabbing id="btn" because I reused this code from another part of the project. This is good and working code.
I have this code in the head:
<script type="text/javascript">
function windowOpen(url) {
window.open(url, 'newwindow', 'width=350, height=450, top=200, left=500'); return false;
}; </script>
and this code down somewhere in the body:
<?php
echo "<i class=\"glyphicon glyphicon-new-window\"></i>"
?>
I've tried it with and without php syntax on both html and php files. The html file always works and the php never does.Any ideas? Thanks, I appreciate your help.
Maybe try something like this...
<?PHP
echo "
<script type=\"text/javascript\">
function windowOpen(url) {
window.open(url, 'newwindow', 'width=350, height=450, top=200, left=500'); return false
};
</script>
<a data-rel=\"collapse\" href=\"#\" id=\"btn\" onclick=\"windowOpen('http://www.abcdef.com')\" title=\"Pop-out\"><i class=\"glyphicon glyphicon-new-window\"></i></a>
";
?>
I think your problem is that windowOpen() method isn't being called since it doesn't know what that method is. But if you place it in the same 'file' it should be able to find and call the method.
Never mind, guys. Thanks for your input. I had another bit of JS grabbing id="btn" because I reused this code from another part of the project. The edited code above is good.
The page would reload instead of opening a new window because of this other JS function.
Related
This IS a duplicate from Calling a php function by onclick event, but I have a question on it. The answer I had a question on is by timpanix, and basically it won't work.
He said to execute some PHP code in a On Click event do this:
onclick="document.write('<?php //call a PHP function here ?>');"
and call the PHP function. Yet whenever I try it:
<button id="profileinformationbutton" input type="submit" value="Login" onclick="document.write('<?php profileupdated() ?>');"> Update Profile </button>
it prints out ');" > Update Profile, yet I have no clue why. It is inside of a form, and the PHP function looks like this:
<?php
function profileupdated() {
?>
<div id="profileupdated"> Profile Updated </div>
<?php
}
?>
Why would the code be displaying this? Please help! :) Thank You.
EDIT
The code does not seem to be writing Profile Updated to the page, any idea why?
function profileupdated() {
echo "<div id='profileupdated'> Profile Updated </div>";
}
Also if you only want to print this value to your tag why you're using function? Assign it to a variable.
like
$myVar = '<div id="profileupdated"> Profile Updated </div>';
Then use this variable where you want?
you should echo or return your function body. Carefull! I changed quotes!
Your PHP function is writing to the page already, rendering the document.write (javascript) useless. Try this:
<?php
function profileupdated() {
return "<div id='profileupdated'>Profile updated</div>";
}
?>
I do want to note though that you might want to consider a cleaner way of doing this: If you just want to display a fixed text ("Profile updated"), stick to pure client-side Javascript.
Otherwise (if there's logic to be done server-side), you might want to decouple server and client and use an AJAX call and JSON to transfer your data.
PHP is a server side programming language, you are executing the JavaScript on the client side.
If you want to call a PHP script from your JavaScript you need Ajax, f.e. jQuery.
I have a HTML form and PHP code. In my form, I have a textbox and a textarea. Within both, I have included the "disabled" option. I want both textboxes to remain disabled until the user decides to click the "Edit" button, in which case both textboxes should be enabled so changes can be made and the output once again saved. According to the research I have done, the only way to do this is to use javascript, so I have included the following code within my PHP;
if (isset($_POST['edit']))
{
echo "<script type=\"text/javascript\">";
echo "var oldnotes = document.getElementById('oldnotes');";
echo "oldnotes.disabled = false;";
echo "var record = document.getElementById('record');";
echo "record.disabled = false;";
echo "</script>";
}
I have also tried;
if (isset($_POST['edit']))
{
echo "<script type=\"text/javascript\">";
echo "$('#oldnotes').removeAttr('disabled')";
echo "$('#record').removeAttr('disabled')";
echo "</script>";
}
But no luck :(
I am not receiving any errors, the textboxes just remain disabled after I click the Edit button. Could anyone help me with this? Thanks in advance.
This is a better approach for these kind of problems :
if (isset($_POST['edit'])){
?>
<script type="text/javascript">
var oldnotes = document.getElementById('oldnotes');
oldnotes.disabled = '';
var record = document.getElementById('record');
record.disabled = '';
</script>
<?php
}
<?
<script language='javascript'>
(javascript code here)
</script>
?>
Try use onclick on your Button
<input type="button" name="edit" id="edit" onclick="document.getElementById('oldnotes').disabled=false; document.getElementById('record').disabled=false; return false;">
I hope it helps.
The second approach seems correct, but you're missing ; there. Also, you haven't put any newline characters.
Here's what I suggest:
<?php
if (isset($_POST['edit'])) {
?>
<script type="text/javascript">
alert("Check entry"); // Use this for checking if your script is reaching here or not
$('#oldnotes, #record').removeAttr('disabled');
</script>
<?php
}
?>
You don't need to echo the whole thing. You can simply put it out of the PHP and keep it inside a if block.
Also, I've kept the alert to check if your code structure is proper. If you're getting the alert on clicking the edit button, means you're on the right path. You just need to workout on the JS.
If not, something wrong with your edit button and the form POST
Use single quotes for script type
echo "<script type='text/javascript'>\n";
//javascript goes here
echo "</script>";
use this jquery code:
<script>
$('#oldnotes,#record').attr('disabled','')
</script>
If you want to use JS with PHP you can read here:
how to write javascript code inside php
However: it seems you want the JS to be called on it own accord upon evaluation in the php logic as per the implementation provided.
Technically speaking your JS simply states it should re-enable the controls. Rather add the JS functionality to the OnClick of the submitting control and it should work fine. This is a quicker way of testing the functionality as well.
<a href="javascript:void(0);" onclick="document.getElementById('oldnotes').disabled=false;" >
Once it is clicked it will hide the appropriate control instead of trying to write new JS on the fly that is not executed. This is tried and tested and should work fine for you.
Hope it helps!
you can always make a cookie
use document.cookie()
set the onclick=“” to changing the cookie
and make a script that runs and gets the cookie when you open the site (use window.onload = function(){/*code goes here*/})
Hello can anyone please help me in fixing this code?
1st code
<script type="text/javascript">
if (document.getElementById("tester") != undefined)
{
document.write('**2nd code should be here**');
}
else
{
document.write('<img scr="./img.png" />');
}
</script>
2nd code
<?php while (have_posts()) : the_post(); get_template_part('item-video');
endwhile; ?>
What I want to happen is to insert the 2nd code into the first code. But when I try to insert it, it gives me a blank page.
the biggest thing I've heard is that PHP is server side, while Javascript is client side. Therefore, changing the html page using document.write() isn't going to do anything unless you reload the page. I think you can circumvent this problem tho, using Ajax and the load() function.
What is wrong on this syntax:
PHP
echo '<li onMouseOver="" onMouseOut="document.getElementById(\''.$boxrow.'\').style.display=\'none\';">';
And on HTML page source code:
<li onmouseover="" onmouseout="document.getElementById('evidence').style.display='none';">
IT is working fine, but scripts after this wil causing errors, if I remove this script everything working fine.
First what is below this script and stop working is:
header("Location: index.php?module=service&no=".$_POST['id']."&"); and next after ...
PHP header redirection only works if you still didn't parse any code on your page. Try redirect by javascript instead.
Also it's a better practice not to use inline javascript. Better:
EDIT:
window.onload = function() {
li.onmouseout=function(){
document.getElementById('evidence').style.display='none';
};
};
I'm trying to replace a div's content with a sourceURL and it works in just a flat html setup, but when I try to incorporate it into a php page it doesn't want to work and I can't figure out why.
..and yes I'm loading jquery.
<script type="text/javascript">
function loadContent(elementSelector, sourceURL) {
$(""+elementSelector+"").load("http://forexfbi.com/"+sourceURL+"");
}
</script>
and
echo "<tr class=\"expand-child\"><td colspan=\"11\" height=\"100px\">$name1<br>"; ?> Link 1 <?php echo"<div id=\"content\"></div></td></tr>";
start with checking browser console to see the error. It may be a possible jquery confict in your php file.
Please state the error you found on console. press f12 and click on console.