That's what I have:
LinkName
Here <?=$arItem["LINK"]?> I'm getting some link, and I want to add to the end of this link some extra hashtag parameter #nav-link.
But the main problem is that PHP is executing on the server side, so, when I click the link, I get only link from the <?=$arItem["LINK"]?> (blabla.com). After second click, I have necessary code blabla.com/#nav-link.
So, is it possible to get blabla.com/#nav-link after first click?
Try something to the tune of this:
<script>
var data = <?php echo $arItem["LINK"]; ?>
var yourLink = "blabla.com/"+data;
//more of your code etc....
$('#yourDiv_where_you_want_the_link').html(yourLink);
</script>
Related
So I am trying to get this code to echo my popup when class elements are clicked. I have the code in the head section of my wordpress file but it's not working. Any ideas?
I've even tried moving the variable around and still nothing
<?php
function popCash() {
$doit = "<script type='text/javascript'>
var wid = '111111';
var uid = '111111';
</script>
<script type='text/javascript' src='//cdn.popcash.net/pop.js'></script>";
echo $doit;
}
?>
<script type='text/javascript'>
$(document).ready(function(){
$('.post-thumbnail, .thumb-block, .display-img').click(function(){
var <?php echo popCash;?>
});
});
</script>
I need it to show the popup when the class elements are clicked
Depending on what kind of result you want, you need to use console.log(popCash) or alert(popCash), not echo, which is not valid JavaScript.
Follow-up after comment:
If you're trying to execute that script, it's more like document.write(popCash) that you'd be looking for, but this wouldn't be a very efficient way to accomplish what you're trying to do because you'd be loading up that script every single time you got a click, and accumulating additional <script> elements with every click.
For one thing, it looks like you're assigning values to global variables wid and uid using a script. There's absolutely no good reason to create a new script just to do that. Simply assign the variable directly:
<script type='text/javascript'>
var wid;
var uid;
$(document).ready(function(){
$('.post-thumbnail, .thumb-block, .display-img').click(function(){
wid = '111111';
uid = '111111';
var popCash = "<script type='text/javascript' src='//cdn.popcash.net/pop.js'>\n"+
"<\/script>";
document.write(popCash);
});
});
</script>
But then the question becomes: "Why reload the 'cdn.popcash.net' for every click?"
Isn't there a method inside the script that can be called whenever it needs to be invoked again?
And even if the script is so poorly written that it must be run from the top for every invocation (that's a terrible API!), you can at least dynamically create and delete script tags to do the job instead of using document.write(), but that would take a bit more explaining to describe.
i´m building a search system for this petshop software web base, the thing is that i search for a name, than send the name with XMLHttpRequest to php page that execute the query and return me the results and display them at my search page,until here is ok. the information that php page returns to search page goes like this:
<?php
$query = "selec ...
$queryName = mysqli_query...
while($fetchNames = mysqli_fetch_array...
?>
<a class="profile">
<div id="clientInfo"><?php echo $fetchNames[0]; ?></div>
</a>
<?php }
?>`
i try to acess the html class .profile imported on searchpage.php:
`<script>
document.querySelector('.profile').addEventListener('click',function=(){
alert('js code works!');
});
</script>
</body>`
i tryed to import it with the results of php query page right after the end of while loop, even with the window.onload=func... it won't work!
javascript won't work at the imported document, it can't see the class to display the alert. how can i work around this issue?
thanks in advance!
As you have dynamically-generated elements, you'll need to make use of event delegation and target an element that exists on page load, and work down from there. You haven't mentioned any parent elements in your question, so I'll target <body> in my answer, as this always exists on page load.
As you're shifting the eventListener up the hierarchy, you need to ignore clicks other than the desired element. This can be achieved with event.target and .contains():
if (document.querySelector('.profile').contains(event.target)) { }
So your final code would look like:
const body = document.querySelector('body');
body.addEventListener('click', function() {
if (document.querySelector('.profile').contains(event.target)) {
alert('js code works!');
}
})
The above ensures that when you click on your dynamically-generated elements your alert() will fire, but it won't fire when you click on any other element.
I can suggest two different methods. The first one is that you can write javascript codes after the html code. or wait for the document to be loaded using 'document.addEventListener('DOMContentLoaded', ...)'
document.addEventListener('DOMContentLoaded', function() {
document.querySelector('.profile').addEventListener('click',function=(){
alert('js code works!');
});
})
I have a page that started with an empty PHP var. Then, when the user clicks on a link I want to pass the value of the name attribute to a JS var, and then pass that JS var to the same page and then update the PHP var with the JS var value. Here is a simple example of what I am trying to do:
// PHP
<?php
// Starts empty, then when posted should update
$jsVar = $_POST['jsVar'];
echo $jsVar;
?>
<!-- HTML-->
<!-- Link to click with name vlue to pull-->
Click Me
<!-- JAVASCRIPT -->
<!-- Import jQuery -->
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
// On link click
$('a').click(function(e){
// Prevent link default behavior
e.preventDefault();
// Store name vale as jsVar
jsVar = $(this).attr("name");
$.ajax({
type: "POST",
// url: "" <--------------- From what I read, if you want to post to the same page you just don't inclue the URL?
data: {jsVar : jsVar}, // <--------- This was also something I found online that supposedly helps pass JS vars to PHP?
success: function(data)
{
alert("success!");
}
});
});
</script>
A lot of people say that I don't need AJAX for this. The example I provided is a much similar version of what I am trying to do, which is dynamically change an include file based on the user clicking a sidebar item. I don't want to use a form for this, do I?
Ah! As I continued to look, I found that jQuery has the .load() function which is EXACTLY what I wanted. Dang! I was able to build this and make it work:
// Store clicked page
var currentPage = $(this).attr("name");
// Build filepath
var currentPagePath = "pages/" + currentPage + ".php";
// Find the div I want to change the include for and update it
$("#pageContent").load(currentPagePath);
So easy. Dang. Thanks all.
I have a php file which is part of a basic Content Management System. Inside this file I have a table which contains a "Delete" link in every row.
What I am trying to do is create a choice popup in a javascript function of which I call when the link is clicked.
My function code (which appears in the head part of my php file) is below:
<script type="text/javascript">
function delete(skillID)
{
var answer = confirm("Are you sure you want to delete this record?");
if(answer == true)
{
window.location.href = "process/deleteRecord.php?skillID=" + skillID;
}
}
The code which calls this function is below:
echo "<td>Delete</td>";
The problem is that the box doesn't even popup, so the function isn't called properly.
Can anyone help?
If there are any other details you need to know please let me know.
You can try something else :
echo "<td>Delete</td>";
You need to use the "onClick" attribute, try that :
echo "<td>Delete</td>";
Hope it'll help!
I have a php code, which is in a div. It shows a random text placed in the random.txt file. I want to refresh this div, to load new text in every - let's say - 15 seconds without refreshing the whoe page.
I've found several solutions to reload divs without refreshing a page and it seems it can be done with AJAX or JS, but they only refresh it with a specific content or a specific file, however I can't figure out how to insert this code and have it refreshed.
This is how my div looks like:
<div id="randomtext">
<?php
include_once("GetRandomText.php");
$MPTextFile = "random.txt";
$MPSepString = "*divider*";
$MPTextToHTML = false;
MPPrintRandomText($MPTextFile, $MPSepString, $MPTextToHTML);
?>
</div>
It loads a random text to the div at every refresh.
I tried to refresh the div it with the JS below, but it's not working at all. Sorry, I'm not good in it.
<script>
var auto_refresh = setInterval(
function () {
var newcontent= '<?php
include_once("GetRandomText.php");
$MPTextFile = "random.txt";
$MPSepString = "*divider*";
$MPTextToHTML = false;
MPPrintRandomText($MPTextFile, $MPSepString, $MPTextToHTML);
?>';
$('#randomtext').html(newcontent);
}, 1000);
</script>
<div id="randomtext"></div>
Thanks in advance.
PHP Script and Javascript are executed in separate environments. (Your server and user's browser, respectively.)
What you need to do is to move the embedded PHP script into a separate PHP file. And using somthing like this:
content.php
<?php
include_once("GetRandomText.php");
$MPTextFile = "random.txt";
$MPSepString = "*divider*";
$MPTextToHTML = false;
MPPrintRandomText($MPTextFile, $MPSepString, $MPTextToHTML);
?>
Inside your html page
<script>
var auto_refresh = setInterval(
(function () {
$("#randomtext").load("path/to/content.php"); //Load the content into the div
}), 1000);
</script>
<div id="randomtext"></div>
Document for jQuery Load function: http://api.jquery.com/load/
As far as I understand you, you are trying to execute your PHP in a browser which is not possible. Your server parses the PHP and replaces that code with the random string.
You need a AJAX request to your server which evaluates this script and sends it back to the client. In a simplified way:
request = new XMLHttpRequest();
request.open("GET", "my.php");
request.send();
$("#randomtext").html(request.response);