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>
Related
I'm still a learner on PHP.
I have a button, and it shall run a JavaScript after clicking it, but I want it to show an alert / pop up for users to confirm whether to continue or to abort.
Can I do the function in my PHP file in the button code?
<TD style='text-align:left;padding:0;margin:0;'>
<INPUT type='button' onmouseup="javascript:JS1();" value='JS1'></INPUT>
</TD>
you need to make function and use confirm();
<TD style='text-align:left;padding:0;margin:0;'>
<INPUT type='button' onmouseup="myFunction();" value='JS1'></INPUT>
</TD>
And
<script>
function myFunction() {
confirm("Please confrim");
}
</script>
Writing code is not what is supposed here.
But, still following pseudo code and concepts should work.
Steps:
If you are using jQuery, bind the event of click. If you are using core Javscript, use onclick attribute.
In the onclick function, add a confirmation popup.
confirm() returns true if user selects yes, or else it will return false.
Now, you have to proceed only if true is returned by confirm().
And javascript code:
<script>
function JS1() {
if (confirm('Your confirmation message')) {
// Write your code of form submit/button click handler.
}
else {
return false;
}
}
</script>
Have you tried bootstrap modal?
https://www.w3schools.com/bootstrap/bootstrap_modal.asp
https://getbootstrap.com/docs/4.0/components/modal/
https://getbootstrap.com/docs/3.3/javascript/
I have provided 3 links. I think these might help you a little. Do a research and it won't be very hard. Good luck.
I'm trying to delete files manually on my site. I created a list of all the files that can be deleted and each file has an input button. I already made a sort of connection with jQuery.
The problem lies in the fact that the button doesn't recognize it's parents hence it doesn't send any information.
This is my PHP of the delete function:
if(isset($_POST["destroyFile"])) {
$filename = $_POST["remove_file"];
$file = 'views/home/added_files/'.$filename.'.php';
$this->query("DELETE FROM page_context WHERE file_name='$filename'");
$rows = $this->resultSet();
unlink($file);
}
I call each row with an for loop and this is the echo of it.
echo "<li name=''>$filename[$i]<input type='submit' name='destroyFile' value='Remove File'></li>";
and it's not much but this is my jquery which changes the name of the parent which is li. Doesn't seem it's connected to it. It just changes the name and nothing more.
$("li").on("click", "input", function(e) {
e.preventDefault();
$(this).parent().attr("name", "remove_file");
alert("hey");
});
The jQuery above makes the name for the li and that name should be called after destroyFile isset.
Is this even feasible? Because php is server sided and jQuery is more website sided coding.
Note: I made an input field where I could type in the file name. This worked. So the PHP code is fine and dandy.
Note 2: This is a bad habit of mine but I re-use $variables. $filename in my php code isn't the same as the $filename in the echo. Just a warning in advance.
You specified the submit button with:
<input type='submit' name='destroyFile' value='Remove File'>
This means, that you can access the value of the specific button in PHP with $_POST['destroyFile'], since you named the input 'destroyFile'. Its value is always 'Remove File'.
To provide an specific file you can set the value of the button to $filename[$i] instead.
echo "<li>remove file <button type='submit' name='destroyFile' value='{$filename[$i]}'>{$filename[$i]}</button></li>";
Calling $filename = $_POST['destroyFile']; will now save the specific filename into the variable.
I have this code:
echo "<form action='activity1.php' method='post'>";
echo "<input type='checkbox' name='checkbox_test[]' value='1'>aaa";
echo "<input type='checkbox' name='checkbox_test[]' value='2'>bbb";
echo "<input type='checkbox' name='checkbox_test[]' value='3'>ccc";
echo "<br><br>";
echo "<input type='submit' name='activity1' value='Activity1'>";
echo '</form>';
This will results 3 checkboxes and 1 summit button. The selection will be handled by acvitity1.php.
I would like to add another submit button for each checkbox line something like this:
echo "<form action='activity1.php' method='post'>";
echo "<input type='checkbox' name='checkbox_test[]' value='1'>aaa "."<input type='submit' name='activity2' value='Activity2'><br>";
echo "<input type='checkbox' name='checkbox_test[]' value='2'>bbb "."<input type='submit' name='activity2' value='Activity2'><br>";
echo "<input type='checkbox' name='checkbox_test[]' value='3'>ccc "."<input type='submit' name='activity2' value='Activity2'><br>";
echo "<br><br>";
echo "<input type='submit' name='activity1' value='Activity1'>";
echo '</form>';
If the user press the activity2 buttons, how can i pass the value another php file (for ex activity2.php) ?
So how can I put a form into another form ?
Think about a table / form where you can select any line for delete (activity1), and buttons for the end of each line to edit the table row where the button has pressed (activity2).
Thank you!
Post edit:
As I am unable to comment, I was unable to ask for clarification. You said:
Ammadu: after pressing activity2 buttons the page get (needs to be) redirected to another page (activiy2.php). At activity2.php i want to catch checkbox_test[] value with $_POST.
In that case, AFAIK, you can not explicitly redirect the request to activity2.php as form nesting is not allowed and "Activity2" submit buttons will always POST to activity1.php. The simplest thing you can do is check which submit button POST-ed the request and react acorrdingly (code for checking the POST variable is shown below in the pre-edit section).
Pre-edit:
Your questions seems a little bit unclear to me, but I'll try to answer it based on what you wrote at the end of the question:
Think about a table / form where you can select any line for delete (activity1), and buttons for the end of each line to edit the table row where the button has pressed (activity2).
Also, I am no professional, merely a student, and an amateur programmer.
I had a similar problem while attending web programming course at my college. Specifically, there were table rows with checkboxes at the end of the each row for deleting the rows and the "Edit" button next to each of them. There was a "Delete" button below the table that was used to call the script that would remove the rows that were marked for deletion. We were using some dirty, dirty hacks to make that work.
You asked about form nesting, quick Googling revealed that form nesting is not a valid code.
Unless you really need to do your tasks in a separate PHP script I would suggest checking the POST variable to see which button was used to POST the form data to the server:
<?php
if (isset($_POST['activity1'])) {
//code for activity1 button
}
elseif (isset($_POST['activity2'])) {
//code for activity2 buttons
}
?>
This approach is also causing another problem - there is no easy way to identify which row that button belongs to. What you can do is dynamically name the buttons in the process of creation for each row (activity2_1, activity2_2...) and then create a loop in the PHP script that would check which button was clicked, which is a very ineffective way of doing things. That was the dirty hack I used back when studying WP course.
What I would go for are simple anchors. You can create them inside a PHP loop like this:
<?php
//...rest of the code in the loop...
echo 'Edit';
//...rest of the code in the loop...
?>
The script activity2.php should then perform a simple GET check and do the rest of the job:
<?php
if (isset($_GET['rowID'])) {
//activity code
}
?>
If you really need to use the buttons:
You can style the anchors using CSS to look like and behave like buttons and then use the code shown above;
...or, if you are allowed, you can use simple JavaScript code that you can generate inside the PHP loop in the same manner, like this (out of my head) and then echo it:
<?php
//...rest of the code in the loop...
echo '<input type="button" onclick="location.href="/activity2.php?rowID=' . $your_row_id . '";" value="Edit" >';
//...rest of the code in the loop...
?>
Hope this helps.
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>";
I'm trying to call a JavaScript function through PHP and have met some problems. I have got three code snippets for your understanding:
1) My javascript function:
function addPoints(radiobutton){
//code
}
The parameter is an actual button and inside the function is a lot of code reading button value and name and taking care of checked status of the button.
2) My php-code creating the button looks like this. Notice that i send 'this' to the function.
echo "<input type=\"radio\" name=\"X\" value=\"Y\" onClick=\"addPoints(this)\"/>";
3) Finally I have this code at the very end of the document for triggering the javascript function when page is loaded.
echo "<SCRIPT LANGUAGE='javascript'>addPoints();</SCRIPT>";
If addPoints only consisted of an alert, this would work. But my problem is that I need to send an actual button as parameter to the function. I need both triggering the function on page load (to load some data from a database) and the normal button onClick-event.
Is there any solution for this if I don't use another server request to catch the desired button? It's important that I get the button created above (in fact I've got a lot of buttons, but let's think of is as one) and send it as parameter.
Give the button to pass to the function on load an id="initial_radio_button" attribute, and then make your last snippet:
echo "<script type='text/javascript'>addPoints(document.getElementById('initial_radio_button'));</script>";