Assign ajax Response to PHP variable to use if further [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a ajax response, which contains 3 variable(a,b,c=array() ) in json Array,
i want to assign c to a php variable so that i can use it further.
Is it Possible if so then how can i do it?
I want to use this array of php to refresh a list In my View.

By the time an AJAX call has returned a response to the browser, the PHP code has finished executing, so you will not be able to run any more PHP after this except by making another AJAX request.
If the html that makes up the list that you want to refresh is produced by a PHP view then you have two options.
1) Run the PHP code before your AJAX response is returned. That way your ajax response could be something like:
{"a":value,"b":value,"c":array,"d":"<ul>... updated list html code</ul>"}
You can then use JavaScript to replace the old HTML with your refreshed view stored in d.
2) You could store the value of c in a JavaScript variable where it can be re-used in a future AJAX request which will return the view HTML.
Hope this helps.

Related

How to call php script using jquery and terminate jquery script? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to execute a PHP script using jQuery and terminate jQuery at that point itself. I want the PHP script to be the last page on my browser rather than the jQuery page. I looked it up but every answer included the variable 'data' in it which I don't need. I'm not fetching data from PHP. I want to directly jump from jQuery to PHP and then end the program on the PHP file itself (I don't want to go back to the jQuery page to return some value from the PHP program). How can I do this?
I assume what you want to do is switch to the page ending .php instead of carrying out an ajax request to it and using the result. That doesn't require jquery and is simply done by setting globalThis.location or its href property like one of the following:
location = 'adjacent_file.php'
location.href = '/from/root/distant_file.php'
window.location.href = 'https://some.site/path/specific_file.php'
window.location = '... like the others ...'
Which of the above you pick is up to your needs and variable scope, but setting window.location will probably do you fine.
If you want to make a POST request that lands on the target page, there are a number of good answers already available for this other question - but you will need to create a form through vanilla JS or jquery and then submit it against the target page.

Which is faster ..passing variables to a javascript function or running a MySql query to fetch the values? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I wish to know the following: Which one of the following is faster- passing variables to a javascript function and then passing it on to a php file to carry out some operation or running a MySql query to fetch the variables in the php file without passing them? ..Thanks a lot!
For pretty similar request .. if the related value are already available to te client and you need this to some other page then tecnically
the javascript ajax request involve only the internet transfer of the call and the transfer of the result .. the use of database access in addition to sending the request and receiving data also implies the access to the database so normally should be more fast ths ajax method
Unless you have a specific reason for using Javascript, for instance, updating displayed records without reloading the page, use PHP directly. You can always run the php function in a class on a separate file if you wish to keep your html and php separate.
My personal experience of ajax, which you should remember still has to access PHP in order to execute server-side requests, is that it can appear slow. Whether or not it actually is slower, it leaves the page in place whilst executing the code and so might present that illusion to the user.
Also, I think the simplest solution is the best, so if you don't need ajax you shouldn't use it, but if you do need it then there is no reason not to do so that I am aware of.

AJAX using PHP - Different ways to structure? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Have a question. I am learning AJAX right now and I see that on w3schools the way they have the PHP and MySQL AJAX stuff as examples is the AJAX makes a call to a separate PHP file on the server and then that PHP sends back whatever you want to pull. The example I see is a table is returned with rows and columns based on the query.
My question is, does using AJAX with PHP and MySQL require you to set it up this way (meaning sending data to a separate php page and then getting a return on the current page the AJAX was called from/started at)?
I have a project where I currently have multiple php pages. Some of them being only 'processors' where they take in data and process it but do not spit out any html/text for the users and I have redirects set up to take the user back to a page, BUT then some of my php files have both html AND php handling the processing and spitting out stuff for the user to see on that page.
Ideally I would like to use AJAX and for the pages that have both HTML and PHP (user facing stuff on the page) I would like for AJAX to call the page its on and spit out the returned data from the database. It is not a hard requirement but think it would save me time not having to refactor a lot of the structure, but if that is the only way or is a better design/practice then I will surely go that way.
Hope this question made sense, and as always thanks for all help and ideas.
I'm not 100% sure about your problem here, but I think a good way to organize your AJAX/php code is to create a kind of php API ( in a php file ), with a bunch of functions doing all the back end stuff, and then, in your AJAX calls, you specify which function you want to use.
Hope it can helps a bit ;)

Saving all javascript code in an online database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am really new to making an async game and I would like to know if you could just get the current gamestate and save it as it is or if I would have to grab all code seperatly and organising them into tables. It would be very useful if there was some kind of function that could save everything at once as I am building a pretty big game and it would take a long time grabbing every last piece of information.
So to summarise: I would like to grab the existing gamestate and put everything in a database, then grab everything again and restore that gamestate.
Thanks in advance!
You'll need to submit (or retrieve) the gamestate data to a server with JavaScript. You can use something like JQuery's ajax function for this.
For the server to do anything meaningful with your data, you'll need to use some kind of scripting language. PHP or Node.JS might be a good place to start if you're new to this.
Once the server gets the HTTP request, you can have it send a query to your database (like MySQL) and send back some kind of response.

How to talk to a database from Javascript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want variables from my Javascript functions to be recorded in a database on my server and then posted back onto the html page. I know this may be easy with the POST and GET method, but I've been reading and I'm stumped. So if anyone could lead me to a sample that shows how to do this or show me here in an example of one that would help.
You need to use some form of server-side scripting language (such as PHP) to capture the data sent from JavaScript (presumably via means of AJAX's XMLHttpRequest object) and insert it in the database.
i.e.: There's no means of directly communicating with MySQL from JavaScript itself - you need to use a server-side scripting language to do this.
You might wanna read into Ajax which uses the XMLHttpRequest object to do exactly that.
Using this you can send data to and from a server side script. So that way you could "insert variables into database."

Categories

Resources