What should I look into to accomplish this.
When you select an input field some text to the right shows up.
For example: https://twitter.com/signup
Anyway i need something like that works with PHP. What should I look in to?
And also How can you query the database and not have to reload the page to see result? For example i have seen on many sites registration you can check if the a username is used without the page reloading. Dont know how to explain better.
Thanks
Like Arkain said, they are making the text appear with JavaScript. PHP is server-side only, meaning it can't make any changes to the page once it has loaded.
You can however, call a PHP script dynamically (to check if a username is registered) using a technique called AJAX.
I'd look into Ajax using jQuery. I had done some things with ajax before trying jQuery but jQuery made it so easy that I found it enjoyable to keep implementing things using it.
You need to use JavaScript, look at this source for instance, for some beginner tutorials.
Learn JavaScript. You can start with jQuery (a pre-made javascript toolkit of functions that help you do many things without reloading the page).
Google for a jQuery Ajax tutorial.
Related
I'm trying to emulate the upvote/downvote system used on the SE sites. Each of my pages have a score which users can upvote or downvote.
The arrows are images with onclick links to javascript functions. I need to find a way to dynamically change the score without refreshing the page and then run a script (probably PHP) to increment the score in the server's data files.
Is javascript the best way to do this? I'm not that big of a fan of letting users see the source for my functions.
This is only possible using Javascript.
Don't worry about users seeing the source; as long as the server is secure and well-designed, it won't do any harm.
You should only implement display and validation logic in Javascript; everything must be validated again on the server.
Welcome to AJAX.
The easiest way to do that is to use jQuery and its $.ajax method.
See http://api.jquery.com/jQuery.ajax/
It's as simple as
$('a.upvote-button').click(function() {
$.ajax('/posts/123/upvote', {type: 'post'});
return false;
});
Yes, you need javascript (or something considerably less sane) to do this. Namely, you need an AJAX callback.
I'm not that big of a fan of letting users see the source for my functions.
The client-side source of your "function" would be ridiculously simple. It could be as simple as:
$.post('/1234567/vote/up')
You may use a direct link to a php script (and form post values) instead, and without a javascript library it would be a few more lines, but you shouldn't need to expose anything of value in your javascript.
Use javascript. JQuery is an excellent choice for manipulating on screen content and interacting with a server via asynchronous calls.
I am getting started with HTML and PHP. Doing alright, I understand the syntax and everything. But... what I know is simply how to build page elements that may interact with other .php files in my site. But what if I want to interact with other elements in the same page?
This is what I'm trying to achieve:
I got some text and a button. I want to press the button, and that will cause my text to become a textbox which I can edit, and afterwards press the button again to convert the textbox back to plain text.
That is what I don't quite get: interacting with other elements to change their attributes/values, seems to be little documentation about that, or I am looking in the wrong place. If that is the case, can someone point me to the correct learning source?
Thanks.
You need javascript for that.... There are loads of examples online and everywhere:
http://www.w3schools.com/js/tryit.asp?filename=tryjs_dom
You are looking at the wrong place as you can't do that with PHP. Changing elements on the client side will involve JavaScript. There are a lot of ways to change elements with JS and to get a quick start I'd suggest you take a look at jQuery, a JS library that will make it easy for you to access and change elements.
What you're looking for is client side interaction. Javascript is the ticket for this and you'll have to include a tag in the head of your html doc.
Look up javascript on w3schools.com and then learn jQuery which is a great implementation of key javascript principles with a lot of extra features like animation and client/server side interaction.
You are moving into the realms of Dynamic HTML. The easiest way I have found to manipulate elements within an HTML page is to use JQuery.
PHP is a predominantly a server scripting language and has very little impact on browser-centric behavior. HTML is a markup language, but with few exceptions isn't dynamic. You want to look into content/browser scripting languages - depending on your medium, the two more popular for the time being are Javascript and Actionscript.
PHP does not allow you to change your HTML dynamically. As the other people mentioned, you need Javascript for that.
What you could do is reload the page on button click and change a session variable (for example) to decide wether the text is displayed as a textbox or just text.
I have a simple php code which prints data from a SQLite database.
Basically $query ="Select A from B".
This all works fine. & when the sqlite db updates, i can refresh the page & the new data displays.
Want i am aiming to achieve is to refresh this data automatically every 5-10 seconds without having to reload the entire page.
I am also trying to avoid using iframes as there is about 20 of these on the page all displaying different data.
This has been driving me mad the last few days, Does anyone know of a way to do so?
My thought have been javascript, jquery or AJAX?
Other than that can you get sqlite data with javascript alone, without php?
& then implement something like the below on the element only? without the page reloading??
setTimeout("location.reload();",5000);
Thank you in advance.
You can do it using setInterval(), passing a jQuery ajax command and the time.
Or take a look at this jQuery plugin http://plugins.jquery.com/project/ekko
I'd recommend using jQuery as it's easy to do this sort of thing with the built in ajax function. Download jQuery and embed in your page or embed via a CDN.
1.) create a PHP file that outputs the content you want to "update" every few minutes. No headers/footers, etc.
2.) Put the content you want to refresh in a div with some specific id.
3.) Check out the simple example for using setInterval and ajax/load on this page.
Ajax is what you need.
Two options that I use quite a bit, both with plusses and minuses:
Jquery, easy to learn, quick to deploy, and very configurable
XAJAX -- AJAX for PHP. It's not the best thing out there, but if you're scared of Javascript, this allows you to do AJAX from PHP Functions, which can be easier for PHP guys to understand.
In essence, output your code to a specific DIV, then use AJAX to update that div with data it queries from the DB.
I need to have a button that calls a php function to resort displayed data from the db. How the heck do I do this? I just found out that because php is server side this sucks. So any help please?
You can't directly call a PHP function pressing a button for the reason you stated yourself. In order to execute PHP code you need to make a new request to the server. The request could be made to another script or to the same that produced your page by calling it again with some parameter to control its behavior.
Alternatively, you can call a PHP script via Javascript (AJAX) so that you can handle its output and update the page without a full reload.
The second option is neater but more complex, the first one might look less pleasing to the eye, but works regardless of the user's browser having Javascript enabled or not.
It should probably sit inside a form field, something like this:
<form action="YOUR_PHP_SCRIPT.php">
<input type="submit" />
</form>
When the submit button is pressed, the action for the form is triggered.
There may be a swathe of other things you'll need to take into consideration from this point onward, but this is a start.
Yeah because PHP is server-side, you have two options. One is to make a button that calls the PHP script and renders a completely new page. The other is to use AJAX (asynchronous javascript and XML) on the page, see jquery.com for a good way to do that, and only re-render the table that is displaying data.
This is a job for ajax, as others mentioned. If I may elaborate, if you're starting out, I HIGHLY recommend using a javascript library to make your life easier. With prototype, here's what your button might look like:
<input type="button" id="button_foo">Button</input>
Here's what your javascript might look like:
$('button_foo').observe('mousedown',function(e){
new Ajax.Request('handler.php',{
method:'post',
onSuccess:function(t){
$('table_bar').update(t.responseText);
}
});
});
This may seem a little daunting at first, but I think basic js has a pretty manageable learning curve when using a library. The above code would take whatever handler.php outputs, and replace the contents of an element with and id of "table_bar" with the returned html.
if you do decide to use prototype, the docs are really helpful and easy to understand, and there is a really excellent book by pragmatic press on the subject that'll have you understanding it very quickly.
Hope that helps!
what Yacoby said, you'll need to use AJAX to make the call to the server, or something like this: http://www.ajaxdaddy.com/demo-sorted-table.html
I was wondering if there is a way to use php to return the values from a search without having to reload the whole webpage or using iframes or anything like that. I've tried searching for it but I always end up with AJAX and I was wondering if there is a PHP way for it...
I suggest you read up on AJAX and what it is, as it is exactly what you are describing.
What AJAX is generating a request on the browser with javascript, sending the request to a server, generating content with whatever technology you want (be it PHP, .NET, etc.) and returning it to the browser, without the page ever 'reloading'. That's all it is, and that's what you want.
I recommend you check out something like jQuery as it is far away the most popular javascript library. It makes doing AJAX requests a piece of cake.
AJAX is what you're looking for. It means using JavaScript (on the browser) to initiate a request to the server (which may be running PHP, or any other language).
PHP is a server-side technology, and what you describe is mostly a client-side issue.
Every technology that does what you want is going to be very close to Ajax, so I suggest to just take a little time and get yourself going with Ajax. There are plenty of javascript frameworks around that make life easier for you as an Ajax programmer.
PHP is server-side. It can't do anything unless a web request is made (i.e. the user clicks on a link, requesting a page). This is why AJAX exists. The javascript on the client side is able to initiate a web request in the background and decide what to do with the response.
Check out jQuery. It makes AJAX a snap:
http://docs.jquery.com/Ajax
Yes I did the same using PHp and Mysql. What you can do is first create a PHP search page1 with a text box and write down some jQuery function for the onkeyup event of text box. Pass the value of the Text Box to the PHP search page2 and display its data in another blank DIV tag on your search page1. Let me know if you were able to get the concept, else I will forward you some link for that. infact I found a youtube video for this. Its not a difficult task.