I have a PHP echo statement in which I create an HTML button element, and within that button element I'd like to set the onclick attribute to the location.href function to redirect to another page when the user clicks the button. But I can't seem to get this working, nothing happens when the button is clicked. I think it has to do with the multitude of single and double quotes but I'm not sure. Here's my latest attempt, but I've tried escaping the inner quotes a number of ways.
echo "Commissioner Admin</th><td><button type='button' id='adddirectoradmin' value='adddirectoradmin' width='75' onclick=\"location.href('http://some-url')\">Add</button></td>";
location.href is not a function. Your quotes are all OK.
Your problem is that you're calling location.href instead of just assigning it a new value:
onclick="location.href = 'http://some-url'"
try this:
echo "Commissioner Admin</th><td><button type='button' id='adddirectoradmin' value='adddirectoradmin' width='75' onclick='". location.href('http://some-url')."'>Add</button></td>";
Related
I am using an onclick event of a input button whose code is written inside php.. code is written below
this select() function isn't calling when i'm clicking the button. even i tried to show alert message on button click .. it also not working. kindly help ...
<?php
echo"<td width=14% align=center><input type=button value=Export onclick=select() /></td>";
?>
Assuming select() is a PHP function, no. PHP is executed on the server side, whereas the HTML onclick event is interpreted by your browser, so one cannot access the other. Have the button link to a new PHP script instead. The new script can then call the select() function.
Just change the name select to someother name like myselect and it should work.
You should not have the function name as select
This should work for you
<?php
echo"<td width=14% align=center><input type=button value=Export onclick=myselect() /></td>";
?>
<script>
function myselect()
{
console.log('Clicked');
}
</script>
echo "<td><a href='edit.php?corp_no=".$query2['corp_no']."'>OK</a></td>";
echo "<td><a href='delete_form.php?corp_no=".$query2['corp_no']."'>Cancel</a></td>";
For above case, I wanna know how to write it with button tag?
It's important to remember that a button 'does something' and a link 'goes somewhere' (actually changes the URI).
Currently there is no way to accomplish what you want via a button.
You can however pass information to a server within a <form> by using the value attribute on a <button>.
Perhaps an even better solution would be to style a <a> (link) to look like a button.
Either format your <a> tags with CSS to look like a button and call it a day, or you'll be using Javascript to accomplish this.
<button> tag:
echo "<td><button onclick='window.location.href=\"edit.php?corp_no=".$query2['corp_no']."\";'>OK</button></td>";
Php can be written directly with HTML
<button type="button" formaction="delete_form.php?corp_no='<?php echo $query2['corp_no'] ;?>'">Cancel</button>
The same method can also be used for OK button
My code has a text input and submit button which on return hides that form and displays a new button, which works. The problem I'm having is setting the value of the button (or innerHTML) to the answer in my query (which will always only be one). I have the following code:
echo '<form><button id="HCP_Btn" name="HCP_Btn" style="display:none"></button></form>';
$HCP_num = $_POST['HCP_num'];
$HCP_Query="SELECT * FROM HomeCareProviders WHERE Number='". $HCP_num."'";
$HCP_result= mysql_query($HCP_Query) or die(mysql_error());
if (mysql_num_rows($HCP_result)==0){
echo 'Sorry there are no Home Care Providers with the number entered.';
}
//HCP_Btn.innerHTML='.$row["name"].';
else {
$row = mysql_fetch_array($HCP_result);
echo '<script type="text/javascript">
HCP_Btn.style.display="";
document.form.HCP_Btn.innerHTML='.$row["name"].';
</script>';
}
You can use this Javascript code
echo '<script type="text/javascript">
document.getElementById("HCP_Btn").style.display="";
document.getElementById("HCP_Btn").innerHTML="'.$row["name"].'";
</script>';
For first change it like this
echo '<script type="text/javascript">
//HCP_Btn.style.display="";
document.form.HCP_btn.innerHTML=\''.$row["name"].'\';
</script>';
for second check if $row["name"] gives you the right value and at last check you javascript console for errors.
Also HCP_Btn.style.display=""; mean nothing like this.
The problem is probably because Your button's id is HCP_Btn but in the JS further You are accessing it like HCP_btn - the problem could be small b. Also You are missing quotes for the innerHTML value.
Change the line
document.form.HCP_btn.innerHTML='.$row["name"].';
to
document.form.HCP_Btn.innerHTML="'.$row["name"].'";
^ ^ ^
make the b uppercase add quotes ----------^
EDIT: Have You ever tried jQuery? It is commonly and widely used JavaScript framework that makes JS programming so much easier (after You know it)... With jQuery, You could just do:
echo '<script type="text/javascript">
$("#HCP_Btn").css({"display":""}).html("'.$row['name'].'");
</script>';
How is the information from your JavaScript call returned to the innerHTML itself? When does it get called and changed? I think you should do that first.
You have an user pressing a button. Then you go to the database using PHP (need a new request response for that), with AJAX or JavaScript you could make it work client side.
I think that you are mixing up server side and client side issues. You should at least need a function call on the onClick event to toggle the display of the button and show the information. That onClick event should call a JavaScript function and that will handle the change.
I have a php function which gets data from database and calls a javasript function like this:
echo "<p>$header</p>";
The problem is when someone hovers over the link, the browser shows the complete parameters like this:(on bottom left of the browser, or a new tab)
javascript: customm('some secret code here');
Is there a way to hide this? I tried adding onclick to the php function and pointing ahref to #.
echo "<p><a href='#' onclick='javascript: customm('$variable')'>$header</a></p>";
But it didnot work.
Thanks in advance!
It's not working because you need to escape the quotes:
echo "<p><a href='#' onclick='customm(\"$variable\"); return false;'>$header</a></p>";
This code worked:
echo "<p>$header</p>";
Thanks KaeruCT and Blender!
echo "<td><a href='delete.php?id=$row[id]&&category=$a' onclick='return confirm(\'are you sure you wish to delete this record\');'>delete</a></td>";
Above is the code I am trying to use. Every time it does nothing and I cannot see how I can use 'proper' JavaScript methods. What is the reason?
It is also a bad idea to use GET methods to change state - take a look at the guidelines on when to use GET and when to use POST ( http://www.w3.org/2001/tag/doc/whenToUseGet.html#checklist )
I think $row[id] is not evaluating correctly in your echo statement. Try this:
echo "<td><a href='delete.php?id={$row[id]}&&category=$a'...
Note the squiggly brackets.
But THIS is much easier to read:
?><td>delete</td><?
As an aside, add a function to your js for handling the confirmation:
function confirm_delete() {
return confirm('Are you sure you want to delete this record?');
}
Then your onclick method can just be return confirm_delete().
Just a suggestion, are you using a framework?
I use MooTools then simply include this script in the HTML
confirm_delete.js
window.addEvent('domready', function(){
$$('a.confirm_delete').each(function(item, index){
item.addEvent('click', function(){
var confirm_result = confirm('Sure you want to delete');
if(confirm_result){
this.setProperty('href', this.getProperty('href') + '&confirm');
return true;
}else{
return false;
}
});
});
});
All it does is find any "a" tags with class="confirm_delete" and attaches the same code as your script but i find it easier to use. It also adds a confirmation variable to the address so that if JavaScript is turned off you can still send them to a screen with a confirmation prompt.
You should try to separate your JavaScript from your HTML as much as possible. Output the vanilla HTML initially and then add the event to the link afterwards.
printf('<td><a id="deleteLink" href="delete.php?id=%d&category=%s">Delete</a></td>', $row["id"], $a);
And then some JavaScript code somewhere on the page:
document.getElementById('deleteLink').onclick = function() {
return confirm("Are you sure you wish to delete?");
};
From your example however, it looks like you've probably got a table with multiple rows, each with its own delete link. That makes using this style even more attractive, since you won't be outputting the confirm(...) text over and over.
If this is the case, you obviously can't use an id on the link, so it's probably better to use a class. <a class="deleteLink" ...
If you were using a JavaScript library, such as jQuery, this is very easy:
$('.deleteLink').click(function() {
return confirm("...");
});
echo "<td><a href='delete.php?id=$row[id]&&category=$a' onclick='return confirm("are you sure you wish to delete this record");'>delete</a></td>";
Use Firefox's HTML syntax highlighting to your advantage. :-)
Another solution:
echo '<td><a href="delete.php?id=' . $row[id] . '&category=' . $a . '" onclick="return confirm(\'are you sure you wish to delete this record?\');'>delete</a></td>';
I changed the double quotes to single quotes and vise versa. I then concatinated the variables so there is no evaluation needed by PHP.
Also, I'm not sure if the return on the onclick will actually stop the link from being "clicked" when the user clicks no.
And if you insist on using the echo-thing:
echo "<td><a href='delete.php?id=$row[id]&&category=$a' onclick='return confirm(\\'are you sure you wish to delete this record\\');'>delete</a></td>";
-- because the escaping is treated from the php-interpretator !-)
Here is what I use for the same type of thing.
I do not echo/print it, I will put the html between ?> html
?> <td>Upd / Del</td> <?php