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.
Related
I have variable in local procedure that rendering data to pop-up window
I'm using oracle apex 20.2
Oracle Database 18 c
Variable name :l_factor_count
I want to pass this variable value to be used in Javascript function inside this procedure
After I did some research I found that this variable is local and needs to be related with page item but I don't know how do that because I use the procedure to do all the work and rendering data in pop-up as I said before.
There are 2 ways of doing what you want
Divide you code in 2 parts.
1º Execute the PL/SQL required sending the values e returning them accordingly. Submitting this values in a dynamic action is APEX 101, so you should be fine here.
2º Execute a second action with the JavaScript code grabbing these values.
Execute an Ajax Call
Use apex.server.process to call you PL/SQL function and make it return your values to continue the process.
I do not advise you to my JavaScript and PL and if possible try to avoid it.
If you don't know how to do the steps from either item, then you might be doing stuff that, at the moment, is to advanced for you. So please check out some tutorials, anything online is going to help a lot.
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
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.
I have a html form in a Smarty template. A select in the form is populated via AJAX. I would like to specify in the Smarty template, the format to be returned by the AJAX-call. This way I can use the same request-url for different purposes. For example if the select contains users, sometimes I might be interested in knowing the user's email address (format: "{$Option->GetName()} ({$Option->Email})"), and other times the user's organisation (format: "{$Option->GetName()} ({$Option->Organisation})").
I have a few ideas of how to achieve this:
Store the format in html/js in the template. Then the AJAX-call can send the desired return-format along with the request. Drawback: A malicious user can change the format in the request, which might be a security issue.
Pass along the entire $Option object, and do the formatting using JavaScript. Drawback: Cannot use the PHP object's methods, such as $Option->GetName().
Store the format in a separate template file, and reference to this file in html/js and send along with the AJAX request. Drawback: The reference to the template file can be changed by a malicious user, but probably no damage can be done. This approach requires extra template files, and the presentation logic will be spread over different files.
Create a Smarty plugin that automates the process of item 3. Then the presentation logic will not be separated. Drawback: The reference to the template file can be changed by a malicious user, but probably no damage can be done.
Do anyone have any other/better ideas, or any experience with doing something like this?
I don't think you need to set some format. You can create a javascript function with 3 parameters. The <select> tag identifier, the name of the key used for options value, and the key name used for options text. The function will make the ajax request. That way you can call the function with settings you need for particular situation.
Further details depend on your current code.
I ended up using option 4, and I'm very happy with the result.
If anyone come across this and need information on how I implemented it, just post a comment, and I'll try to explain.
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