PHP MYSQL Updatable Form - php

I am trying to create a form that you can enter data into mysql using php and then it will update the table on the same page instantly here is what I have
Index_test.php: http://pastebin.com/03fndSHG
Update.php: http://pastebin.com/jQraSskS
index_style.css: (http://)pastebin.com/PhYxttFu
When I submit this I get a double entry in my form and I want the update to be seamless like on the index_test.php it shows a line that says "An entry has been added" and the table refreshes automatically without any movement to another page. I ahve tried finding something with Ajax but nothing I try works

If I were doing this, I would use jQuery for the ajax. There's lots of documentation here: http://docs.jquery.com/Main_Page. It simplifies ajax, if you know how jQuery works. To use jQuery, you'll need to know some javascript as well. Without jQuery, just javascript & php is enough, but trickier because internet explorer does ajax differently than the other browsers.
Without ajax, you should probably submit the form to the same page as the form, which then redraws itself with the new table row. Even with ajax, if the user turns off javascript, then the form needs to submit to the same page, or another page that has the form in it. This is known as progressive enhancement or graceful degradation, meaning, the web page still works if javascript is disabled.

Related

Laravel: Displaying data from db after clicking a button - but staying on the same page

That's the task I am currently facing:
Starting point: I have a form, which accepts data and saves it into the database.
What needs to be done: I need to include a button called "show data" (a different one than the "submit" button in the form, it can even be a clickable div), which will then display the data from the database on the same page, under the form. The button should also enable refreshing the list after new data was inserted into the form.
Now, if the data from the database had to be displayed on a different page, that could be handled easily with routing. However, for this particular task, I need to display the data on the same page.
My question: Is there a pure Laravel/PHP way to solve that problem? I am considering using AJAX (to which I am also new), but if there is some Laravel specific solution then I would prefer that.
The solution needs to work on localhost.
For PHP, without having any help from JavaScript, showing any changes to the database will require a browser refresh. On the front-end, you can use VueJS and Axios to help you with these requests, which do not require the browser to be refreshed.

curl to submit post data to inputs only appearing with form submission

I'm writing a crawler in PHP that will enter data into my website. I need to submit data in order to my inputs to display on the page (they're not just hidden). Normally, there is an AJAX process that runs on a button click to display these inputs.
I have returned an HTML doc with these inputs in there; however, my problem is that I can't curl again with my data before these inputs go away. I've tried using curl_multi_exec and it doesn't seem to help.
Also, all of this is at the same URL (form action and form I want to submit).
For Ruby or Perl, the Mechanize library is great for this scenario. For PHP, we want to find a framework library similar on PHP, which leads to this Q/A:
Is there a PHP equivalent of Perl's WWW::Mechanize?
Enjoy.

Can't post data to self because of design

i have a website that uses a number of containers (div's). In this scenario, we have three boxes down the left side and one bigger box on the right of these boxes. The bigger box on the right is the only thing that changes when a link is pressed, this is done with Javascript.
SO i have the index which is the main layout of the website and i also have the "pages" of the site, so when a link is pressed the main box (on the right) loads the new data.
On one of my pages i want to collect data and then run it through a PHP script that is in the head of the file, but when i click the button i realise it refreshes the whole page and it doesn't run the script on the page with the form.
Anyone had a problem like this, or know of something i could do to work around it?
I'm not really sure what code would be useful for helping me but if you need something just ask.
Thanks for the help
Since you are loading all your content via JS already, you could just POST the form data via AJAX to a PHP script to process, then read the output and either provide an error message or remove the form from the page and show your success message.
How to approach your AJAX call is dependant on what you've used as a basis for the rest of your JS.
Personally I like to use the JQuery library, as it makes AJAX calls (and much more) very simple.
How about you make the page design able to do it. Have the backend be able to spit out the state of the page when it posted.
Or use Ajax to post the data back and set the new state like you do already.

Is my logic correct? Ajax and/or PHP with Mysql

I have a page which shows a list of items. Page coded with html, css, php and using mysql db.
On that page a user can request to add one of the items to their special list.
I want to do this within the page without having to do a complete page refresh. So user clicks button to add, item is added to their list and button changed so they can't add it again.
Do I use ajax calls to run code behind the page and then refresh the div?
Or is there a better more efficient way to do it.
I'd prefer a php option of possible in case user has js turned off, but don't know if it can be done with using js.
Any help appreciated.
If you want dynamic content (changing the page without refreshing) you are going to have to use Javascript. To do what you are asking, you could call a PHP script via Ajax that outputs the contents of the div with the new item, and then change the div based on that response.
Dagon is exactly right. Create a form which handles the request and set the action of the form to the PHP script you want to handle the request. Note that although this can be the same php script that you use to process your ajax request, it does not necessarily have to be.
Many times when I implement such functionality, I'll set the PHP to send variables as POST (in the event of JS disabled) and have my ajax request as a GET so I can use a single PHP page to handle the 'same' request. When using AJAX, I'll have the script echo a specific code then have the ajax response handle that return.
if(val == 'OK') {
//in event of success, perhaps you want to hide the original form and show a success message
} else {
//do something like unhide a hidden div to display an error
}
If JavaScript is turned off, the page has to be reloaded. In your case jQuery could be very handy and simply rewrite the element you need to rewrite. The server send's a simple json. Using a PHP Framework might also be a good idea, since the way you ask it seems (with respect, and not wanting to offend), that you are not using any framework and might run into falls making your script vulnerable (sql injections for example)
If your visitor doesn't have JavaScript enabled and you want to serve anyways, then you have to do a page reload. First check if that is worth to do, who is your client/visitor, what browser do they use, ... questions like that help you to design your page/app.

HTML toggle button for simple site using php possible?

I've got a simple website using plain HTML/CSS to display and PHP/MySQL for data storage.
Now I'd like to add a toggle button similar to facebooks "like" button.
How can I act on the user pressing the button (add database record for this item, change button text) without leaving the page?
I thought this question would have been asked and diskussied to no end, but all solutions I found require some other frameworks than plain PHP as background.
You'll need to do it with javascript. Read up on "AJAX form posting".
A high level view:
user clicks on button
you capture the click via an onclick handler in javascript, and use it to call a javascript function
said function does a remote url request via XmlHttpRequest to a target page
that target page takes in the parameters passed via POST or GET and performs actions with them (eg add database record), and prints out any response required
the javascript function reads the response and acts accordingly (eg change button text)
and all this happens without refreshing the page.
You can do all this with pure low level javascript code, but plenty of libraries already abstract it while solving various issues with browser compatibilities. I'd suggest the jQuery javascript library. It provides an easy way to do exactly what you require, and good documentation.

Categories