Showing html form data on subsequent pages - javascript

Page 1, complete a HTML form. Submitting form links to page 2.
Page 2+, shows a story populated by the answers to the HTML form.
I can't find a why to bring back elements of the HTML form to complete gaps in future pages. Should I be looking at trying to use Javascript or to learn some PHP to store and then re-use the form data?

Yes, you will some sort of server side programming, store data locally and then retrieve it with javascript or passing the information to the page using query strings. Maybe other options are possible but those are the things that come to mind right now. Have a look at this answer for an example of the last method I mentioned.

Related

Using JavaScript function result in PHP

Is it possible to use the results of a JavaScript function in PHP?
Let me break down why. I have a table that has several columns, including a column that gives each row a checkbox. When a user presses an edit button, an onclick event is activated. The onclick event does a few things, but primarily it goes through the table and figures out which checkboxes are clicked, and returns an array of numbers which indicate the IDs of the entries of the selected rows.
What I want to know is how can I take these Ids and use them in my inline php code that should use the Ids to get corresponding data from a database and generate a new table.
Also, is there another way of doing this type of thing? This seems a little convoluted. I've tried XMLHTTPRequests to get the data directly with Javascript but I had trouble/couldn't get past using the GET method of an XMLHTTPRequest. My code would execute, but I wasn't sure how to get/use the data.
Any help is appreciated.
You can't use javascript values in your PHP statements because, imagine that the javascript is a client-side language, so it don't communicate with the server, and imagine that PHP is a server-side language, so it can't communicate with javascript. When you request a webpage, the request go to the server and the server will trait your request, and all output of this request (the webpage) is a code on client-side language and it is rendered with javascript, html and css. So, we can say that javascript have no idea of what is PHP, because, what PHP do is write a full html code and send to your browser render like a response. But you can use Ajax inside your client-side, there is a lot of tools developed in javascript that do the work of make requests to server-sides

How can I display and update PHP objects in HTML?

I know similar questions have been asked and answered before, but I see so many different answers that I'm wondering if somebody can give an answer for my specific issue.
I wish to display a list of Soccer players on a webpage, which are retrieved from a database. Each player needs a drop-down box, allowing him to be picked/unpicked/subbed in the squad. When this happens, the database needs to be updated, along with the webpage, to show his new position and to store the information. I know how to update the database with PHP, but the only way that I know how to do the webpage part, is with Javascript. I thought I'd cracked it when I fathomed out how to use 'onchange' to call a Javascript function, but that's brought up another problem. If I pass the ID of the player and his new position to the function, I'm unable to update the PHP object from inside the Javascript, because there's no way (that I know of) for the PHP code to read the passed variables. What I'd want to do inside the function, is take the passed ID parameter, use that to match up the player in the PHP array, then update him.
The only solutions that I can think of are either
A) Don't use PHP at all in the HTML page and instead pass the information to the page with json_encode... presumably then read it into a Javascript array/function. Then when the function updates the player, call an PHP file (can parameters be passed into the file this way?).
B) Somehow convert the parameters in the Javascript function to PHP. Is this possible?
C) Find another way to respond to the down-down box that calls PHP instead of Javascript. Again, not sure if this is possible.
D) Call a PHP function from within the Javascript function. Again, don't think it's possible?
Is there a simpler solution or do I go with one of the above?
Javascript should be used to enhance the user experience. It should not be mandatory for your page to work.
First do your page with HTML and PHP. HTML passes your form data via post to your PHP script. PHP then populates your database with that data.
Then add javascript if you want to enhance functionality.
With javascript you can do things like validate the form so that you don't need to send the page to PHP until is it properly validated thus by saving you a page load. Note: You should still validate using PHP afterwards, javascript is only for enhancement.
You can pass the data from javascript to PHP using ajax.

Dynamically / iteratively build up page content

I am planning to create a website which will let you iteratively construct an SQL query.
The idea is the following:
while(user wants more where clauses)
{
show selection (html select) for table columns
let user choose one column
upon selection, show distinct values of that column
let user choose one/multiple value(s)
}
I know how to handle the SQL part, but I am not sure how to tackle the iterative building of the page.
So my questions are:
What is the best method to build the page iteratively with the idea sketched above?
What do I do, if the user changes one of the previous selections?
The website will be build with Perl and I am thinking of utilizing Ajax for the dynamic part.
Any help is much appreciated.
If I were to do this, I'd use SQL::Abstract.
UPDATE:
If you don't want to redraw the whole page, you're going to be using AJAX. So find yourself a JavaScript library that you feel comfortable with that includes ajax calls. Jquery has this, a bunch of others do too. People have differing opinions on various libraries.
Anyway, your workflow looks like this:
user submits form
javascript performs client-side validation
javascript submits AJAX-style to the server
Server performs server-side validation, data manipulation, etc.
Server responds with data paylod
client updated the screen based on the contents of the payload.
So let's concentrate on 5 and 6.
Data Payload: The X in AJAX means XML, but many apps send back JSON, or HTML.
Update the Screen:
You can apply HTML directly to the existing page by setting a tag's innerHTML or outerHTML attribute. But that doesn't update the DOM. If you don't dig around the DOM in your clcinet code, then this can suffice. If you dig around, then you need to build nodes and add them to you page's DOM, so you might want to consider sending back JSON or XML.
So let's say that you have a div with id='generatedSQL' when your AJAX call retruns, it will fire a callback method (let's call it updateSQL()) and inside the callback you'll have code that looks approximately like:
$(#generatedSQL).innerHTML = theVariableHoldingTheHtml;
Your other option is to parse the JSONXML/etc. and using createNode(),etc, build new DOM bits and plug them into your page. I don't have the jquery chops to write this for you. I look it up every time I need to do something like it.
If the query text is only ever display-only, and you never try to dig around in it on the client side, just use the innerHTML method, whether you pass HTML or pass JSON and generate HTML from it. If the query text is important to the inner workings of the client, then you'll need to write bunch of DOM manipulation code.
No sure what you would be using but, if uses clicks on something then use the Onload event of the element with ajax call to script which brings back the data/content and on readystate just update the innerHTML of the container DIV.
Hope following link will help you understand the concept will give you a code to start with as well.
http://www.w3schools.com/php/php_ajax_database.asp

How to handle non-trivial form input? JSP or Javascript?

I'm working on a Spring MVC app, which should let user alter contents of a list.
The list consists of Book-objects with simple properties like name and author. The view is a JSP page that displays the list of books and lets the user alter the contents.
Altering the list can mean adding books, removing them or changing the order of the books in the list.
Question is, how do I get the altered list back to the server? I can write JavaScript to control the list, but how do I post it to the Spring controller? On the other hand I can write a JSP form for altering the model which would be trivial to submit back to the server, but am I then limited to basic text fields in form input?
EDIT:
In JSP it is very easy to alter a single model's properties using a form like
<form:form action="myaction" method="post" commandName="mybook">
but if your model is a (ordered) list of objects then how do you edit it?
In Javascript I can get the list of objects from the response and change it as needed, but how do I submit it back to the server? Something like
$.post("/modifybook.do",{ name: "Spring in Action", author: "Graig Walls" } );
works, but only for single objects.
Having never used Spring this may well not be appropriate but could you not convert the list into XML and then post that back to Java and then work on it retaining all intricacies and changes?
You should avoid manipulating the whole list, period. I can't see a scenario where populating entire list of items and sending it back to the server is desired, when only one list element is being changed (edited, added or removed).
What I usually do in my app is I create a handler (controller) for returning a whole list of objects in one go and then add another handlers for adding, editing and removing single entries within that list. I also try to stick to REST in such scenarios, so that I have a clean API which represents server resources and the frontend (AJAX + jQuery) makes use of it.
This solution works really well for me so I suggest sticking to it as well.

client-side data storage and retrieval with html and javascript

I'm building what I am hoping to be a fairly simple, quick and dirty demo app.
So far, I've managed to build a bunch of components using only html and javascript. I know that eventually I'll hook-up a db, but at this point I'm just trying to show off some functionality.
In the page, a user can select a bunch of other users (like friends). Then they go to a separate html page and there is some sorting info based on the selected users.
So my first attempt was to put the selected users object into a cookie, and retrieve the cookie on the second page. Unfortunately, if the user changed their selection, the cookie wasn't getting updated, and my searches on StackOverflow seemed to say that deleting and updating cookies is unreliable.
I tried
function updateCookie(updatedUserList){
jQuery.cookie('userList',null);
jQuery.cookie('userList',updatedUserList);
}
but though it set the cookie to null, it wouldn't update it on the second value.
So I decided to put the selected users object into a form. Unfortunately, it looks like I can't retrieve the contents from the form on the client-side, only on the server-side.
Is there another way to do this? I've worked in PHP and Rails, but I'm trying to do this quickly and simply before building it out into something larger and am trying to avoid any server-side processing for now, which I have managed to do up to this point.
Since this is a demo, can you use HTML5? If so, you can use local storage: link text.
Another option is to use AJAX to load the other HTML page (replace whole body of the current document). Your storage variables would be stored in the <head>. This is a tightly coupled design, but again you're making a quick and dirty demo.
Is updatedUserList a string? If it's an array you might have to stringify it first:
jQuery.cookie('userList', JSON.stringify(updatedUserList))
(and of course parse it when you're retrieving it.)

Categories