tr link with php code inside - javascript

I'm trying to do something a little weird and I'm kind of lost.
I have this code:
<tr onclick="href='<?php if (isset($_SESSION['id_usuario'])){ echo 'logado.php?'; } else { echo 'index.php?'; } ?>grupo=<?php echo $row_buscainterna['id_grupo']; ?>'">
My <tr> are in hover, so when the user clicks it I would like to perform this "php action"
Can't make it work. Can somebody help me?
What do you think about it?

I think it would be easier to put a link inside your tr tag, and style it to make it the size of the tr.

You would like to use code like this:
<tr onclick="window.location.href='<?php if (isset($_SESSION['id_usuario'])){ echo 'logado.php?'; } else { echo 'index.php?'; } ?>grupo=<?php echo $row_buscainterna['id_grupo']; ?>'">
Here is an example

OnClick attribute should contain javascript code. Your example sets global variable href to some URL.
If you want to send the user to this URL, then you shoud use document.location instead of href:
<tr onclick="document.location='<?php if (isset($_SESSION['id_usuario'])){ echo 'logado.php?'; } else { echo 'index.php?'; } ?>grupo=<?php echo $row_buscainterna['id_grupo']; ?>'">

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")';

Writing the code correctly - Javascript [duplicate]

I came across a problem which I can't solve on my own!
I have a Table which is populated using PHP/MySQL something like
foreach($users as $user){
?> <tr id="<?php echo $user['fName'].
"-".$user['lName'];?>">
<td> <?php
echo $user['fName']." ".$user['lName'];
?> </td>
<?php
for ($i = 0; $i<$count+3; $i++){
?> <td><input class="accessCheck" id="<?php
echo $user['lName']."-".$pagesArray[$i];
?>" type="checkbox"></td>
<?php } ?>
</tr> <?php
}
As you can see table would look something like
fName lName | checkbox | checkbox | checkbox | mainCheckbox
What I need to do is to check/uncheck all checkboxes with mainCheckbox, is there a way to select all rows just from checking this mainCheckbox?
EDIT: How to find class when you select something like input, is there a way of finding a class for this? For example
$("input").click(function(){
});
Is there a way to find a class for this input?
Tyr this:
$('#all').on('click', function(){
$(':checkbox').prop("checked",$(this).is(':checked'));
});
http://jsfiddle.net/Dfzdh/
I think you would need to use jQuery
Look at the sibblings method.
Here is a question close to yours.
OnClick change all sibling table rows jQuery
I havent heard about any "onChange"-equivalent function in PHP yet

inserting popup window command in sql

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
?>

How do I embed Javascript in PHP code?

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*/})

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