i'm just beginner and i want to call function which will be create the "sub buttons" when button is clicked, how i can do it from this block in echo? help pls
<?php function btncreate($filename) {
echo "<button class='m-1 btn btn-outline-success'>$filename</button>";
}
?>
P.S. i need to do it at the same page with the general buttons
i was trynna to do it by using onclick, putting the JS code inside the echo
First thing first, why do you need to create UI using backend language like that's mostly a Front end language job, Secondly even if you want to do it like this then I say you should use AJAX but that still requires you the combination of Front END with Backend, and if you are looking for a simple button creation upon an event and then save/retrieve something for that button in the backend then I think we can start with JavaScript to create the button on an event (i.e. CLICK) and upon its creation just run an ajax request to save/retrieve whatever you want from that.
Related
So basically my question is very simple, I have two buttons, I for page forward, one for page backwards, If one of those is pushed, a javascript function is called inside an onClick Event. Javascript then gets the variables of the page and then redirects to the next page, the only problem is, that I need to pass those variables to PHP in order to put them into the Database. So for that I make a load of cookies to pass the variables.
However, I was wondering if something like this would work :
<form>
<a onClick="nexpage();" onSubmit="phpScript.php"> <img src = "previous button.jpg"/> </a>
</form>
The idea behind this is that I want to store the variables in a PHP script, which will put them in a display:none; <div> and then for javascript to get the variables out. This instead of using cookies.
So is it possible to run a PHP script to get the variables and when the script is finished to get them, Javascript kicks in to redirect to the next page...
The reason I don't test this at this moment, is that my code is 100% complete, I don't want any sudden changes that maybe won't work at all. Yes I know back-up this and that, but I thought just asking here, maybe someone will know the answer!
Sincerly,
Harmen Brinkman
You can also use onClick = "this.form.submit(); return false;".
There is no any event like onSubmit for link, instead form do have onSubmit event.
Normal Way as OP asked.
<form action = "phpScript.php" method = "POST">
you can use document.getElementById("my_form").submit();
#Dipesh Parmar – Good point. You could also do:
window.onload=function() {
document.getElementById('my-form').onsubmit=function() {
// do what you want with the form
// AJAX POST CALL TO PHP PAGE
// Should be triggered on form submit
alert('hi');
// You must return false to prevent the default form behavior
return false;
}
});
Inspiration by Capture a form submit in JavaScript
I have a php application in which the web page displayed to the user. The page has some links "Edit", "Rename", etc.
When the user clicks on the link a dialogbox prompts. The dialogbox is nothing but a HTML <div> form that gets instantly displayed when the user clicks on the "Rename" or "Edit" link.
When I looked at the html source code (i.e. view -> source in Internet Explorer) I found the following Javascript and HTML code
<a class="update renameButton" href="javascript:void(0);">Rename</a>
I'm unable to understand how the dialogbox gets promted with the above code.
I expected the code to be something like the following:
<a class="update" onclick='rename();' href="javascript:void(0);">Rename</a>
Can someone help me understand this?
Some JavaScript loaded from a <script> element probably binds an event handler function to the element.
The event handler is most likely bound to the element elsewhere (from an included JavaScript file perhaps). For example:
document.getElementsByClassName("update")[0].addEventListener("click", function () {
// Do something on click of the first `.update` element
}, false);
you should not setup event listeners in html anymore like with onclick.
the page registers an event listener to the Object. e.g. with a library like jQuery.
You are absolutely correct! That is very natural to expect such a thing except that there are other ways to bind an event to an object as well.
If you check the JavaScript code on the page I am sure you will find perhaps something that looks like $('a.renameButton').click(function(){}); (if the site is using jQuery) or something similar that binds the onclick event of that particular tag to perform some specific actions.
I'm trying to learn how make an AJAX script
for a LIKE button, on my website. I have the following questions:
if i'm sending 1 variable.... id.. I do this
data: "action=vote_up&id="+(this).attr("id")",
is this syntactically correct if i'm sending two variables id and id1 ?
data: "action=vote_up&id="+(this).attr("id")&id1="+(this).attr("id1")",
2) What goes into the href attribute? The php page or the AJAX?
<img scr="like.png">
3) which is run first.. The php page or the AJAX.
4) Is it mandatory for me to use jQuery or Pure Javascript for running AJAX
thanks for your time and patience. I most appreciate it.
1) Yes, you could simple undestand it as a PHP-Get request to a script, so multiple vars are possible, like Adam mentioned.
2) For backwards compatibility you should just link to a PHP/whatever-Script that provides the same functionality but doesn't rely on javascript (Not everyone has js enabled). In your javascript you just disable the defult click actione ( see: http://api.jquery.com/event.preventDefault/ ) otherwise it you only want to allow the like funktionality if js is enabled than you could just link to the page anchor '#'.
3) The page runs first. It is progressed by the server and than sent to your browser. In the browser the recieved javascript will start its action.
4) Everything you are using in jquery is based on simple javascript functions, but jquery is much more comfortable ;) The equivalent to the ajax method of jquery is XMLHttpRequest ( http://www.w3schools.com/xml/xml_http.asp )
Here is a idea, hope it helps.
If handle_vote.php is the URL responsible for the handle the up vote, you must do two things:
the a href is the URL with the query string for the up vote, your data, is this case. It must be generated for you server application. It will be used in case of no javascript.
you should put you event to handle the up vote in the a onclick event, to send the ajax request, and use the preventDefault jQuery function to avoid the default event. In this case, a href will never be used, the js will suppress the link click.
A code sample will be almost like this, in you php page:
<a class="like" href="handle_vote.php?action=vote_up&id=<?php echo $post_id; ?>"><img src="like.png"></a>
And it as your jQuery script:
$(function() {
$('a.like').on('click', function(e) {
e.preventDefault();
$.get($(this).attr("href"));
});
});
You can personalize as you like, it is only the idea of how to do it.
<img scr="like.png"> put onclick event on that link, and make AJAX request` to increment count, on success response update count clicks on button. And you forgot about one thing, you should save the state of that button. Because one user can go to your site and click 1000 times on it.
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>";
I have created a class named as "member" and inside the class I have a function named update(). Now I want to call this function when the user clicks 'UPDATE' button.
I know I can do this by simply creating an "UPDATE.php" file.
MY QUESTION IS :-
Can I call the "update() function" directly without creating an extra file? I have already created an object of the same class. I just want to call the update function when the user clicks on update button.
an action.php example:
<?
if (isset($_GET[update])){
$id=new myClass;
$id::update($params);
exit;}
//rest of your code here
?>
Your button is in your view. Your method is in your class . You need a controller sitting in the middle routing the requests. Whether you use 1 file or many files for your requests is up to you. But you'll need a PHP file sitting in the middle to capture the button click (a POST) and then call the method.
As far as I know you can't do this without reloading your page, checking if some set parameters exist which indicate the button is clicked and than execute the button. If this is what you are looking for... yes it is possible on page reload. No as far as I know it is not possible directly because your php-function has to parse the results again.
Remember that a Model-View-Controller way is better and that this will allow you to ajax (or regular) requests to the controller-class.
You do it on the same page and have an if statement which checks for the button submission (not completely event driven) like so
if (isset($_POST['btn_update']))
{
update();
}
<input type="submit" name="btn_update" value="Update" />
That will have to be wrapped in a form.
Or you could do it with AJAX so that a full page refresh isn't necessary. Check out the jQuery website for more details.