Wordpress: Querying a database from within javascipt / jquery - javascript

I have a simple postcode search in my wordpress site. The user enters a postcode, clicks search and the browser directs to a new URL based on the postcode entered.
Presently, I have a large switch statement in the onclick handler of the button, which parses the postcode input in a text box and generates some display text and a URL which I then launch using window.open.
It works OK, but as I add more and more postcodes its bloating the code loaded into the browser and it's all visible in dev console. I'm also limited when it comes to making changes as I'm having to go in and modify the code to implement logic directly in my onclick handler. It's a bit of a mess!
What I would like to achieve is to store all of the relevant information in a mysql database, query the DB when the form button is clicked, and then launch the URL which will be returned by the db query (well, there'll be a bit more logic in the server script than that but ultimately I'll just be returning a URL and some text).
I'm struggling to get my head around how to implement this. It appears the DB queries need to be in PHP on the server and has to be queried asynchronously? I'm getting lost down a rabbit hole of AJAX which I'm struggling to understand!
I'm not looking to asynchronously update content on my page - at the point that we're running code in the onclick handler, we're ready to launch a new page based on the postcode entered. It appears the required solution is a little more complex than I hoped it might be.
Can anybody suggest how I might implement this? Do I have to use PHP & AJAX?
Thanks

Related

Cross Domain form submit Google Tag Manager auto trigger

I have some problem while getting data from another site. In this case I want to get the reservation data from the booking engine site and they want to pass the data to me with Google Tag Manager. I don't really understand what I should do when they just need GTM code. What I should create in my server to get the data from the booking engine with Google Tag Manager ?
This is the illustrations:
I have two sites called sites1.com and sites2.com. In sites1.com I put the Google Tag Manager scripts to push form submit data like full name, last name, email, etc. After somebody submit the form I want to get the submited data in sites1.com to sites2.com with Google Tag Manager. My problem is how to get the data after somebody submited the form in sites1.com in my sites2.com ?
Please anybody knows how to resolve my problem . Thanks in advance .
Well if they implement your GTM from site2.com into site1.com all you need to do is:
Create a trigger for the submit button on the form (use the ID o class of the element and check for the that the Page URL contains site1.com/)
Create a tag where you want the information to be send
Scrap the fields with javascript or ask them to push to the dataLayer the information you need (in this case you can build the trigger based on this event)
And SUPER important: check all your triggers so no other tag fires on site1.com
2.1:
Im not sure if i get where you want this information to be stored but keep in mind GA does not accept PII. On the other hand if you want this in some DB you can just create and endpoint and send the information as parameters. Example:
site2.com/booking_info?field1={{DL variable}}
And just use a Custom IMG tag.
Ive made something like this using API Gateway, Lambda and DynamoDB and it took me 15 mins to set up. (just to give you perspective)
-- EDIT:
Ones you have the information avaliable you can send it to your database using two methods:
Using a HTML tag and making a request with javascript
Making a request with a custom image tag
On the past i ve just added the URL with parameters where you want the request to be made on an image tag and worked perfectly for me.
-- More info:
The custom image tag requests an image from a particular URL. The interesting part is that by making the request you’re actually transmitting information to a server. For example, if you request an image via URL https://www.example.com/something?parameter1=good&parameter2=123, the receiving server processes parameter1 and parameter2 and acts accordingly (e.g. registers those parameters as an event).
Source.

Javascript control page and user view page

Currently I'm working on a project where a user enters a lot of data constantly for a hour long window. I'm looking to have one user control all the data via some control panel and then have a link they can distribute to other users that will allow them to view that data without the ability to edit it.
Right now I'm doing some extremely weird methods. I have an XHR request on the control page that fires whenever a field is finished being edited. From there the data is sent to a php file that converts the data into a simple text file. Then the distributed link file will load that file one time and translate it into the necessary format.
Some potential problems I've run into are it seems odd that I'm sending starting as javascript data then going to a php file then to a text file then translating the data all the way back into javascript data again. Another problem I've come into is I'm not sure of a way to force users to reload the page when a field is edited in the control panel after the user has opened the view page.
Have I totally gone overboard here? What are some better concepts I could employ to accomplish this task?
If i understand what you want to do this is how i will do this:
First the data entry
if you have lot of fields you better use a form wizard, i don't have a particular one in mind right now but there is lot of them just search jQuery Form wizard
Here is an example:
http://i.stack.imgur.com/Luk2b.jpg
The concept of the form wizard is to guide user via multiple page and also validate the data. And click save when and the end.
Then save date in database.
Display content
All you need to do is to create a global separate page to display your content.
Let see something like: http://yourserver.com/view/{id}
where id is the identifier of the particular row in your database.
i'm not sure if i totally understand what u about to do. i'm trying to make your work description shorter here:
want to build a website that one person can edit a single page's content in 1 hour, and others can view the content change in that 1 hour.
if this is what u want to build, here's the module:
teacher: the one who can edit the page
student: the one who can only view the page
server: information center
teacher client edits page -> teacher client sends update data to server -> server saves data -> server sends update notice to student client -> student client receives update notice -> student fetches update data from server
to make this module work well, i suggest try socket instead of http reqeust, just like online games or IMs do.
well, try socket.io

Skip to a Page on Google Forms

I'm programatically creating a Google Form using Google App Scripts right now, and my script automatically generates a page for each of the respondants that the form will be emailed out to.
Currently, the form has a dropdown at the beginning that the person uses to select their page, and then it forwards them to it, however, I'd prefer it if the email already sent out a pre-responded form that already started them on their page.
The reason I'm throwing them all into a single form is because I'm using Triggers to detect the form's submission, and since I have more than 20 people that require a form, that would break the trigger limit.
EDIT:
Example code:
the Form.getPublishedUrl() function returns the URL to respond the form, starting on the first page, however, I would like a URL to respond to the form starting on the 3rd or 4th page.
While the FormResponse.toPrefilledUrl() will allow me to autofill the correct response on the first page, it will not allow me to start the responder on the page that he would be forwarded to based on his pre-filled response on the first page.
tldr; :
How do I get a link to a specific page on a Google Form using Google App Scripts?
This should be a comment but don't have 50 rep yet...
Why do you need various HTML pages?
I think you should refactor your code so every form is on the same HTML and everything shows and hides with Jquery or Javascript. You can send the same URL but with differente URL parameters.
If this isn't your case, you should post some code of yours and detail a little bit more your question. Also, wich limit does it triggers?

Display form result in same page with only client-side script possible?

I have a bit of a tricky work problem I hope to get some help with.
To handle various forms and storing entries, we use an in-house tool which I cannot modify. Basically what happens is:
I enter he URL the original page, and the URL of the destination page after successful submission.
The tool spits out some HTML and Javascript code, the most important of which is a unique URL, let's call it (redactedURL), that goes after the action attribute.
When the form is submitted, the page will refresh to one of two possible destination URLs: the one I inputted if success, or (redactedURL) if error.
I can download all the entries from the tool afterward.
The HTML is quite simple. checkform() is a simple validation script.
<form action="(redactedURL)" name="enenForm" method="POST" onSubmit="return checkForm()">
The issue with this is that I can't style the (redactedURL) error page, which is quite ugly. I am wondering if there is anyway I could
Suspend automatic display results of form submission
Determine the destination URL, and based on that, write out a custom thank you/error message (since I cannot access the server-side script, this seems to be the only solution to determine if the submission is successful or not).
Make sure that tool still properly stores all the entries.
Any help would be much appreciated. Thanks!
Don't use a form. Instead use AJAX. I think this SO Question will provide a start. Basically you use JavaScript to submit data to a server using XMLHttpRequest. The returned HTML is a string which you could either modify or (better yet) normalize and add to the DOM.
For an advanced example jQuery-Mobile does this concept when you click a link instead it gets the HTML from the server as an AJAX request copies the HTML inside the <body> and inserts it into the DOM.
Search for tutorials about AJAX and jQuery (or your prefered JS library). Like this one.

How to get bookmarklet running that allows to grab text, load a specific form and past the text into the form

I am very very new to JavaScript and I would like to build a Bookmarklet for a webservice I use. I need to grab text from any page, load the form of the service and post the text into the textfield of the form. So far I got this:
// grab text
javascript:(function(){var t=window.getSelection?window.getSelection().toString():document.selection.createRange().text;t="You selected: "+t;alert(t);})()
//load form
document.onload=function(){window.location=%22http://www.streetmails.com/index.php%3Fc=mailing%26a=content%22})();
//paste text into form
window.onload=function%20D(a,b){c=b.split('|');d=false;for(q=0;q<c.length;q++){if(c[q]==a)d=true;}return%20d;}function%20E(){f0=document.forms[0];f0['subject'].value='news from London';f0['content'].value='<echo$_GET['t']?>';f0[''].value='Add%20a%20picture...';f0['topicId'].value='4';}E
Can you help me? I searched and keept trying and trying but cant get it running....
Thanks in advance!
You can not do exactly what you are trying to do using a bookmarklet. When you execute a bookmarklet, you are executing Javascript inside the current web page. Javascript running in the web page of one web domain can not interact with pages from a different web domain. document.onload will almost never work in a bookmarklet because usually a web page is already loaded before you click the bookmarklet.
Lets call the page with text the "text-page". Lets call the page with the form the "form-page". A web form always submits to another page or to itself. It depends on the form's action value. Lets call that the action-page. Forms can be either submitted via GET or via POST.
To accomplish your goal with a bookmarklet, generally what is done is the bookmarklet will create a form on the text-page and then submit that form directly to the action-page.
If the action-page will accept a GET (only query string values), that is easiest. Look here for many such examples: https://www.squarefree.com/bookmarklets/search.html
If the action-page requires a POST, that is a little more complex because you must use Javascript to create the form: http://www.google.com/search?q=javascript%20create%20form
Addons, extensions, and user scripts can be used to do what you want more directly because they are not limited to operating only in the current domain, but of course they are more complicated to program.

Categories