MrScabby Posted July 2, 2015 Share Posted July 2, 2015 (edited) Hi again all, I think I'm going round in circles here trying to chose a method to populate a form with a chosen name. I also think with a well crafted reply could put me right and save more lost hours. Im new to this area of coding and you could say not practised. So with a few months till my next module I thought I would reaffirm my current knowledge by launching into a project. Lol, even the smallest of ideas can involve hours and hours of commitment. I think though in my current problem Im lost because there are so many answers and I cannot decide what or which to chose. The specification is creating the registration part of a website, New User, Edit User, Delete User - sounds simple but Im trying to make it work similar to a web app so I would expect a lot of interaction between the server and the client - There is a comboBox at the top populated with the 'profile_name' information. I want the user to select a name and the form underneath the combobox to fill with all the other information. as this is only a simple practice I only need a simple form with 5 or so fields per row profile_name,profile_alias,profile_email,profile_phone,profile_mobile in a table called 'profiles'. So Im thinking I will need an 'onchanged' event so when a new name is selected I can call a javascript function to go get the info for the rest of the form, this will be returned by the php script and I then populate the form. And this is where I get stuck - As I re-write the keywords to dig out more articles about moving variables between server and client and vice-versa, the more work I dig up for myself. I mean what is the best/easiest to do? I would have thought something like this would have a simple method or function alas no. Ajax, Json, Cookies, Headers, XML, ??? and so on. I ended up confused and looking at months of learning just to send/receive variables. I think asking is the best thing here, On a different note, do I really have to rewrite the entire form everytime someone selects a different name from the combobox? It would be best if the form remained static and the content of the html boxes changed. Is this possible? and do I have my understanding of the relationship between client and php server wrong here? I was thinking the server had some control over data but if I echo the php results as said in some of the articles then my form is refreshed with a blank screen. I suppose I could change the php to rebuild the form but am I again choosing the hardest method? Thanks in advance, Phil Edited July 2, 2015 by MrScabby 1 Quote Link to comment https://forums.phpfreaks.com/topic/297144-im-going-round-in-circles-with-forms-and-sql/ Share on other sites More sharing options...
requinix Posted July 2, 2015 Share Posted July 2, 2015 (edited) First, New/Edit/Delete user are three different things. Was your description only talking about the Edit portion? Second, I'm not sure about this combobox (I assume you mean a textbox with autocomplete?) being the best way to find a user. If I needed to edit a user, would I want to use a search page first? Often the process is to search for the user to edit, check the search results to see I got the right person, then click a link/button to go to the edit page. Third, AJAX. That's the thing. - AJAX is what lets your Javascript communicate with PHP - JSON is a way of sending data back and forth between the two - Cookies are the things that keep you logged in on websites (among other things they do) but you don't really need to worry about that for this - Headers, again don't need to worry much about those - XML is another way of sending data, but JSON won that battle as far as AJAX is concerned Fourth, rewriting the form. There are a million ways you could do this. What I'm thinking right now is: 1. In your Javascript you keep track of the various form elements and what piece of data they represent (eg, username or email) 2. The AJAX returns a simple blob of data in JSON. Maybe it looks like { "name": "MrScabby", "email": "myscabby@example.com", "phone": "123-555-7890" }3. Use Javascript to update every form element according to the data returned. For example, a textbox corresponding to the name will get the name value. Do this very generically, even if it's just a lot of if the element is a textbox then { ... } else if it is a list then { ... } etc, then apply that to everything you can. Edited July 2, 2015 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/297144-im-going-round-in-circles-with-forms-and-sql/#findComment-1515429 Share on other sites More sharing options...
scootstah Posted July 2, 2015 Share Posted July 2, 2015 I made a quick example showing how you'd use AJAX to populate your form, based on a dropdown box. https://jsfiddle.net/8s608y7j/ To make it work with a PHP script, you could simply replace the getUserById() function to send an AJAX request. Quote Link to comment https://forums.phpfreaks.com/topic/297144-im-going-round-in-circles-with-forms-and-sql/#findComment-1515430 Share on other sites More sharing options...
MrScabby Posted July 4, 2015 Author Share Posted July 4, 2015 Thanks for that, Yes requinex, AJAX does sound the ticket. I was going in circles trying to work out which method to use. When you start looking you get the feeling people have their own methods they prefer, I have read tricks using all sorts of coding techniques and in the end all it served was to confuse me... I'm reading right now, lets hope there isn't too much of a learning curve... Quote Link to comment https://forums.phpfreaks.com/topic/297144-im-going-round-in-circles-with-forms-and-sql/#findComment-1515547 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.