inserting popup window command in sql - javascript

I'm assigning popup window on my links but it doesn't work sorry i'm still learning about mysqli and javascript.
while($row = mysqli_fetch_array($result))
{
$id = $row['BapID'];
echo "<tr>";
echo "<th>" . "<a href=bapview.php?BapID=$id onlick='pop_up(this);'>View Full Info</a>" .
" | " .
"<a href=bapupdate.php?BapID=$id onlick='pop_up(this);'>Edit</a>" . "</th>";
My script
function pop_up(url){
window.open(url,'win2','status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=1076,height=768,directories=no,location=no') }

The url that you want the pop-up window to point should be put as the parameter to your function call in your onclick handlers:
<a onclick="pop_up('bapupdate.php?<?php echo $id; ?>');">Edit</a>
As you can see at
https://developer.mozilla.org/en-US/docs/Web/API/Window.open
the first parameter to the window.open method is a string value for the url you want the opening window to point to. Your code was attempting to provide as that argument the actual reference to the link element on your page (that's what this will refer to in that context).

A couple of suggestions:
Don't be afraid to 'exit' (?>) PHP half way through a script. This will make it easier to read later on and allows you to write in pure HTML.
It looks like your missing you quotation marks around your href parameter. This likely won't cause issues but you never know. I would also recommend using the full url in your href and also as your pop_up() parameter (as suggested by #myesain)
<?php
while($row = mysqli_fetch_array($result)){
$id = $row['BapID']; ?>
<tr>
<th>
<a href=# onclick='pop_up("http://fullurl.com/bapview.php?BapID=<?php echo $id; ?>");'>View Full Info</a>
|
<a href=# onclick='pop_up"http://fullurl.com/bapupdate.php?BapID=<?php echo $id; ?>");'>Edit</a>
</th>
</tr>
<?php
//Rest of code
?>

Related

Cannot echo .load function

For some reason this code doesn't work. Adding \ to the front of the $ doesn't change anything. Can anyone see the problem?
$commentid = 2; //for example
echo "<script>";
echo "<div id = 'commentinput".$commentid."'>";
echo "</div>";
echo "$('#commentinput".$commentid."').load('editcomments2.php')";
echo "</script>";
Do it like this:
echo '$("#commentinput'.$commentid.'").load("editcomments2.php")';
I'd suggest not using echo to output HTML (in most cases anyway), but closing the PHP tag instead.
This becomes (edited with a loop example, see comments below):
<?php while ($commentid = $dbObject->dbMethod()): ?>
<div id="commentinput<?= $commentid ?>">
</div>
<script>
$('#commentinput<?= $commentid ?>').load('editcomments2.php');
</script>
<?php endwhile ?>
Note that in this case, the <script> portion should probably be outside of the loop, with a jQuery selector matching all the comment inputs.
So besides all of the other answers, I found another solution to!
When echoing out js code, like
echo '$("#commentinput'.$commentid.'").load("editcomments2.php")';
//and
echo "alert('stuff')";
it might then print it out like: $("#commentinput2").load("editcomments2.php")alert('stuff') and the code will get confused. So lastly I need to added a semicolon to prevent the program from reading all my code as just 1 line:
echo '$("#commentinput'.$commentid.'").load("editcomments2.php")';

Passing a PHP variable using Javascript Void 0 as the HREF?

I am creating a page that allows a user to edit a user. The table gives me the row ID which is in the button shown below. It works fine when having it direct to another script. However, I wanted to open the users details dynamically which this program does.
Although I don't understand where to place the $row to have the page be able to access it? My research was more about just passing between both programs when I just need to link it up.
I just want this to when the button is pressed, send the $row['usersid'] variable to the page which I can then use.
EDIT: It seems I've confused both myself and you all. I want to keep this in PHP if possible. All I need is the $row['usersid'] or a variable that stores it which is in PHP currently. To be sent to the page without it refreshing.
echo "<td><a href='javascript:void(0);' onClick=".$row['usersid']."' id='showButton'><span class='glyphicon glyphicon-edit'></span></a></td>";
echo "<td><a href='javascript:void(0);'".$row['usersid']."' id='showButton'><span class='glyphicon glyphicon-edit'></span></a></td>";
There are many ways to pass the information in page to another one, let me list some of them:
approach
echo "<td><a href='/path_to_another_php_page.php?userId=$row['usersid']' onClick= id='showButton'><span class='glyphicon glyphicon-edit'></span></a></td>";
then you can access to this information searching in $_GET['userId'].
If your want to use this userId, only for make ajax request, you could do simply doing:
echo "<td><a href='javascript:void(0);' data-user-id='$row['usersid']' onClick= id='showButton'><span class='glyphicon glyphicon-edit'></span></a></td>";
as you can see I added a data-user-id attribute that contains the real userId from php, so in your Javascript you can do this:
document.getElementById("showButton").addEventListener("click", function(){
var userId = this.getAttribute("data-user-id");
// here you can call your ajax ...
}, false);
I hope this be useful for you.
if you want to check your value then use this code which you get your value
you can also get data using this code
<?php
$row['usersid'] = 10; // fot test case
$row['usersid1'] = 13; // fot test case
?>
<script type="text/javascript">
$(document).on('click', '.getValue', function(){
// send data to PHP
var id = this.getAttribute("userId");
alert(id)
});
</script>
<?php
echo "<td><a href='javascript:void(0);' class = 'getValue' userId = ".$row['usersid']." id='showButton'>Button</a></td>";
echo "<br>";
echo "<td><a href='javascript:void(0);' class = 'getValue' userId = ".$row['usersid1']." id='showButton'>Button</a></td>";
this is second option
<script type="text/javascript">
function getValue(value){
alert(value)
}
</script>
<?php
echo "<td><a href='javascript:void(0);' onClick=getValue(".$row['usersid'].") id='showButton'>Button</a></td>";

Clickable HTML list to query db and output to <div>

Bear with me; this is my first StackOverflow question. I'm having trouble writing a proper algorithm. Perhaps doing all this to "force" it means I'm over-complicating a simple concept, but it's also very likely I'm just unaware of the fix.
I'm building an experimental cookbook that reads from a database and displays in the browser. I created a list (note: NOT a <ul> or <ol> element) that is populated by <span> items generated by a PDO query to the database. These spans reference the name of each recipe in the database.
<p>
<?php
$recList = $pdo->query('SELECT name FROM Recipe');
$rowCount = $recList->rowCount();
echo 'There are ' . $rowCount . ' recipes currently in our database!';
echo '<br /><br />';
while ($row = $recList->fetch()) {
echo '<span class="recName"';
echo '>' . $row['name'] . "</span><br />";
}
?>
</p>
I then created a scrolling div element:
<div id="recWindow">
<!-- Display recipe queried from the database here -->
<?php require("$DOCUMENT_ROOT/$rec_source"); ?>
</div>
I would like the user to be able to click on the recipe names generated by php and the chosen recipe to display within the <div>. Choosing different recipes should not cause the page to reload.
I feel like the answer lies in an AJAX request to a php file listening for a variable containing the recipe to display, but that's where I'm stuck. Somehow I need to pass the php list items a unique identifier that is recognized by javascript, which in turn handles the onclick change in the div by passing that identifier BACK to php to query the database. While typing that out, I'm almost certain that I've over-complicated this entire process.
I thought of using a dropdown select menu and a GET request, but I'd like to retain the clickable names function if possible.
Answers that conclude my proposed method is too "dirty" and point me in a better direction are completely acceptable. I'm happy to provide any necessary information I left out. Thank you so much in advance.
Environment: Virtual LAMP (CentOS7, MariaDB)
Try something like this
<p>
<?php
$recList = $pdo->query('SELECT name FROM Recipe');
$rowCount = $recList->rowCount();
echo 'There are ' . $rowCount . ' recipes currently in our database!';
echo '<br /><br />';
while ($row = $recList->fetch()) {
echo '<span class="recName" data-id="' . $row['id'] . '"';
echo '>' . $row['name'] . "</span><br />";
}
?>
</p>
<div id="recWindow">
<!-- Display recipe queried from the database here -->
<?php require("$DOCUMENT_ROOT/$rec_source"); ?>
</div>
<script type="text/javascript">
$(document).ready(function() {
$("body").on("click", "recName", function() {
//* get id of required recipe
var recId = $(this).attr("data-id");
//* send ajax-request to back-end
$.ajax({
url: "/get-recipe.php",
method: "GET",
data: {
id: recId
},
success: function(respond) {
//* put recipe-data into container
$("#recWindow").html(respond);
}
});
});
});
</script>
I hope, it shows you the main idea

PHP - echo JavaScript is not working

Hello everyone !
I'm working on a board based on bootstrap, i've got a database filled with sounds informations (name / genre / link soundcloud ...)
In my PHP page, I echo a button which call a JS function with a PHP argument inside.
echo "<button onclick='SCM.queue({title:'".$name[$j]."',url:'".$liensc[$j]."'});'>Click me</button>";
My problem is, when I look at the source code of my page, I'm supposed to have something like :
<button onclick="SCM.queue({title:'Marvin Gaye - Sexual Healing (Kygo Remix)',url:'http://soundcloud.com/kygo/marvin-gaye-sexual-healing'});">Click me</button>
But instead i've got :
<button onclick="SCM.queue({title:" marvin="" gaye="" -="" sexual="" healing="" (kygo="" remix)',url:'http:="" soundcloud.com="" kygo="" marvin-gaye-sexual-healing'});'="">Click me</button>
So, I'm looking for the right synthax :)
Hope someone can help me !
Thank you all
You quote improperly
Try this
echo "<button onclick='SCM.queue({title:\"".$name[$j]."\",url:\"".$liensc[$j]."\"});'>Click me</button>";
https://eval.in/139055
Try as
echo '<button onclick="SCM.queue({title:\''.$name[$j].'\',url:\''.$liensc[$j].'\'});">Click me</button>';
use
echo "<button onclick=\"SCM.queue({title:'".$name[$j]."',url:'".$liensc[$j]."'});\">Click me</button>";
try this
echo "<button onclick=\"SCM.queue({title:'".$name[$j]."',url:'".$liensc[$j]."'});\">Click me</button>";
Example
Use this:
echo "<button onclick='SCM.queue({title:'{$name[$j]}',url:'{$liensc[$j]}'});'>Click me</button>";
If I isolate the code like so:
<html>
<body>
<pre>
<?php
$name[1] = 'Marvin Gaye - Sexual Healing (Kygo Remix)';
$liensc[1] = 'http://soundcloud.com/kygo/marvin-gaye-sexual-healing';
$j = 1;
$line = "SCM.queue({title:'".$name[$j]."',url:'".$liensc[$j]."'});";
echo $line . "\n";
echo "<button onclick='SCM.queue({title:'".$name[$j]."',url:'".$liensc[$j]."'});'>Click me</button>";
?>
</pre>
</body>
</html>
$line and the button show as:
SCM.queue({title:'Marvin Gaye - Sexual Healing (Kygo Remix)',url:'http://soundcloud.com/kygo/marvin-gaye-sexual-healing'});
Which means your syntax is correct.
I'm wondering if you've somehow managed to enter the line you provided in this example correctly but it is different to the one you're using in your real app!
It may also be worth checking that the $name[] variable is indeed passing normal string data.
Normally, I'd make sure that something in the onclick='' part is a variable as it makes it simpler to read because you're not getting as lost in nested single and double quotes.
eg:
$onclick = "SCM.queue({title:'" . $name[$j] . "',url:'" . $liensc[$j] . "'});";
echo "<button onclick='$onclick'>Click me</button>";
Several of the other answers have suggested escaping the quotes out (with a \") which is good, but its probably still better to not have it inline in the echo statement, unless you're really sure of understanding what you're trying to do!
Edit:
Also see: https://eval.in/139090
for an exapmple

include variable in javascript?

I need some help with the following.
I have a script that close down the web page using the following:
<a href=\"Javascript:void(0);\" onclick=\"parent.location.reload();window.parent.Shadowbox.close();\">
It works as it should. My problem is that I need to change parent to another page. For this I used the following:
<a href=\"Javascript:void(0);\" onclick=\"parent.location='http://www.google.com';window.parent.Shadowbox.close();\">
It also works, but now I do not want it to go to google but one of my pages where it should include a variable. Normally I would use:
<?=$array["id"]?>
But since it is a javascript then I cant use PHP and the following solution does not work:
<a href=\"Javascript:void(0);\" onclick=\"parent.location='new-new.php?$array[‘id’];window.parent.Shadowbox.close();\">
So I tried this:
function getNewURL() {
var root="http://minside.net";
var id="topic/#entry667119";
var nURL=root+"/"+id;
return nURL;
}
With the javascript:
<a href=\"Javascript:void(0);\" onclick=\"parent.location.replace(getNewURL);window.parent.Shadowbox.close();\">
For in this way to get my variables with me, but it does not work.
Someone who can help me? Or have another idea for how to resolve this?
you can assign a php variable to javascript variable.
var a=<?php echo $array[‘id’]?>;
you are using echo for that right since you use \ for ", so it should be like this:
<a href=\"Javascript:void(0);\" onclick=\"parent.location='new-new.php?'.$array[‘id’]'.window.parent.Shadowbox.close();\">
The best way to assign a variable in Javascript is
var id = <?php echo json_encode($array["id"]); ?>
You need to use echo:
<? echo $array["id"]; ?>
Edit
var id = <?php echo $array["id"]; ?>;
var link = "<a href=\"Javascript:void(0);\" onclick=\"parent.location='new-new.php?id="+id+";window.parent.Shadowbox.close();\">";
assuming you use $_GET['id'] in new-new.php (you omitted it in the URL)
Edit
if(strlen($large_photo_exists) > 0 && strlen($thumb_photo_exists) > 0)
{
echo $thumb_photo_exists;
echo "<p>Use this photo as my profile photo</p>";
echo "<br> OR <br>";
echo "<p>Upload and use another photo</p>";
}

Categories