Javascript error document.getElementById('x').style.display='none' - javascript

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';
};
};

Related

using ajax to pass string from php to javascript

So I have a piece of javascript code in an html document that responds to a button click. I want a new url to open, and if I specify the link in javascript as I've done below, everything works fine.
<input type="submit" id="submitbtn" value="Purchase Module"/>
<script type="text/javascript">
document.getElementById("submitbtn").addEventListener("click",handle_click);
function handle_click() {
var link;
link="http://www.google.com";
window.location.href=link;
}
</script>
Problem is I want to hide the real link on the server side as it includes a username:password. The php script below calls a function (not shown) that generates the link (a string). This also works fine.
<?php
$link=get_page_link();
?>
I want to pass the link string to the javascript and have tried various iterations of
link=<?php echo $link ;?> to no avail. As I understand it you can't pass strings this way and you need to use ajax. That's where I'm stuck. Seems like I need a $_POST on the php side and a $_GET on the java side, but not sure on the specifics. Any help would be appreciated. Thanks.

Jquery script doesn't work in jquery loaded page

I have tried to search for similar questions, but I could not find anything, so if you know any similar question please let me know.
Let me explain what Im doing:
I have a script that validates forms in a js file, It works fine in any page with a form I have, the problem is that when I load a form using jquery it just doesn't work, I have tried using the next line in different places: Header, footer, etc
<script src='myFile.js'></script>
By far the only thing that has worked for is writing the line of code above inside the form itself.
I think it has something to do with the form that the DOM works, I have also tried using and not using it.
$(document).ready(function (){ //code});
It only will work when I add the script tag with the src attribute inside the form itself.
It would not represent a big problem for me to add the script tag to any form I load using jquery but it's a little bit more of work an unefficient, and also when I add the script tag to any form and load it using ajax I get the next console warning that only goes away when I remove the script tag from the form file:
jquery-3.2.1.min.js:4 [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.
Here is a part of my code:
<!--home.html-->
<div id='formFrame'>
</div>
<script>
$("#formFrame").load("html/loginForm.html");
</script>
<!--end of home.html-->
<!--loginForm.html-->
<form action='somePage.php' method='post' id='loginForm'>
<input type='email' name='email' placeholder='email'>
<input type='password' name='password' placeholder='password'>
<input type='submit' name='submit' value='Login'>
</form>
<script src='js/validate.js'></script>
<!--end of loginForm.html-->
<!--validation script (validate.js)-->
$(document).ready(function (){
$("#loginForm").submit(function (e){
e.preventDefault();
alert("Working");
});
});
Thanks for spending some of your valuable time on reading this, I appreciate it a lot!
As I can't comment, putting my comment in answer!
I am not sure what you have written in validate.js, but if you are using jQuery unobtrusive validation, then you must rebind the validators to the form if you are loading it dynamically.
I was facing same issue in ASP.NET MVC while loading forms using AJAX. I am not sure will it help you or not but below is the code.
$("#formFrame").load("html/loginForm.html", function(){
var $form = $("formSelector");
$form.removeData('validator');
$form.removeData('unobtrusiveValidation');
$.validator.unobtrusive.parse($form);
});
Well guys I found a solution:
As there seems to be a conflict when loading an external page using ajax (.load) I opted to use php instead of javascript which worked fine:
I removed the next code
<script>
$("#formFrame").load("html/loginForm.html");
</script>
And added the next to the div where I want to load my content:
<!--home.php (changed html to php)-->
<div id='formFrame'>
<?php
require 'html/loginForm.html';
?>
</div>
I would have prefered to use the load method from ajax to avoid loading the content befere the user could request it, but by far that's the only solution I have been able to think about.
Thanks to everybody for your help it was really helpful!

window.open() works in html file, but not in php file

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.

How can I put PHP code inside JavaScript document.write

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.

Setting DIV Content With JS Not Working, So Close

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.

Categories