Connecting Php with javascript - php

I am trying to make a plugin that people can place on their site to generate a form. I dont want to use an iframe to display the form but use javascript to generate it.
The problem is how do i connect the javascript and php together. My site is programmed in PHP.

Your getting a liite mixed up, I think.
PHP runs on your server. This is the place where you fetch data from the database and create some form of html-response.
Javascript runs in the browser. It can't directly talk to your database.
iframe is a special html-element: Therfore it is passive and can't do anything like creating a form.
You have two ways:
Create a PHP script which handles everything through plain HTTP-Requests. This is the "old school" way and requires a lot of page-reloading.
Write most of the logic in javascript and let it communicate to PHP/your database through AJAX. In this case. Have a look at jQuery which makes AJAX-requests (and a lot of other things) very easy.

One issue you will be faced with is 'Cross site Scripting' with Javascript / AJAX.
You can read up on it a bit here:
http://snook.ca/archives/javascript/cross_domain_aj
Also, thinking your process through, you will need sufficient javascript code to create a 'widget' on any place, and have a way to communicate BACK to your server (keep in mind PHP only runs local on your machine, so it cannot be used remotely in your javascript).
You will probably need to build a JSON API (google / stack search this if needed).
And enable communication from your JAVASCRIPT to the API (don't think of it as to PHP, even tho php will be your API server side language).
Here is an example of a PHP JSON API (on youtube too):
http://www.youtube.com/watch?v=F5pXxS0y4bg

If you put PHP into JavaScript and someone implements this, PHP will compile on their server. So you just can't. You just need to put the form in your plugin.

Related

Create plot from database server side and visualize it on client side

I'm currently trying to find the best way of doing this:
We have a python program (client side) we use to upload metrics on a mySQL database on a server and later, via web check it and filter it, etc.
The problem arises when we try to plot any query from to the database. It's unclear to us what aproach to use. The main page was made with Joomla in php.
Currently I was looking into python alternatives to run on the server side, somehow capture the query, process the data, create the image and then return it to the client side as an image or as a string to be reconstructed on the client side. But as I have read it seems also possible (and maybe easier) to do the same in PHP or JavaScript which (as I understand it) run on the client side, leaving less to worry about.
Is it that so? Are my assumptions right? Which aproach would you use/pŕefear? Is there some link or info you could give me to continue my search?
I would prefer to do it on Python using something like matplotlib, plotly, bokeh, etc. but as I see it, the problem is not about creating the image, but about comunicating and sending the image information between server and client.
Thanks!
I think it possible to do what you want to with bokeh. I dont know joomla but it think its not that important.
Check out that part of the bokeh documentation Embedding Bockeh
then for the server parts in python i would recommend flask to start with.
You can just prototype your app with one of the bokeh server apps examples from the bokeh repo.. there are flask examples too.
Then you can start to extend the bokeh server app from the examples with a query to your database with sqlalchemy or
mysql-flask

Asp + vXML dynamic data

I have been put in charge of building an IVR using vXML and asp.net. For some reason the voice server we are using requires ASP.net and cannot use PHP in conjunction with vXML so I am stuck learning ASP.net. The application is pretty simple in that it runs an ASP.net file with vXML and should pull data from a database based on user input.
Example:
User enters customer ID "23313"
It should then pull data from our SQL2012 DB that corresponds to that ID and read it back via prompts. Simple enough I figured.
A have a couple questions regarding possible solutions to this -
Is it possible for ASP/vXML to pull data from PHP dynamically (post or get statements), and use the data in the current vXML document? or will I have to bite the bullet and figure out a second page?
if using PHP is not ideal or possible, would it be better or possible to add a db connection into the asp/vXML document and run the IVR that way?
I am not very familiar with ASP.net, and am trying to find out the most efficient way to accomplish my goal without having to have an additional vXML page to run.
Any help appreciated.
EDIT
After further investigation and help from Jim I was able to get inline PHP working. The server I was using was set to go specifically to this asp.net and did not have PHP installed on the server itself. After installing PHP, changing where the server was looking for the file, I am able to run the latest PHP version in my app.
Deleted code sample as it was completely irrelevant
The ASP requirement seems odd, unless you are leveraging some type of library within the ASP.net environment. VoiceXML browsers, are just that, a browser. It should be able to process VXML from the standard sources. I suspect you are working within a framework that requires the serverside ASP.
If your browser is VoiceXML 2.1 compliant, you should have access to the Data element. This element allows you to make Get and Post requests to a server, get back XML and parse the data within Javascript. Note, the return data must be valid XML.
Any database connection would have to be on the ASP.net side of the solution. VoiceXML gets data by transitioning to a new page (goto or subdialog element) or the Data element above.

Call all php scripts via ajax?

The idea is that I call all of my php scripts via ajax so the php scripts aren't visible when a user views the sourcecode.
Is this a good ideal especially regarding security and performance of my website?
The PHP code that you write will never been seen by the end user - the code is executed by the server and returns everything outside of the PHP tags (<?php ... ?>). The PHP code inside the tags is NOT returned to the browser.
As far as using AJAX is concerned this won't help you with security, your AJAX calls are only as secure or unsecure as standard GET or POST requests to the server. What AJAX may help you with is performance since it allows you to send and recieve data in the background. If your application is data-centric then AJAX will be useful to communicate pure data with the server. If you have a standard hierarchical website design then there's little user for AJAX, just use links to move around and forms to send data.

New Web Developer- PHP or Javascript for user login

I am trying to create a website that will allow users to login with their email and password. To prepare for this I am attempting to learn the languages that will best help me. I have a knowledge of HTML/CSS and am wondering whether to learn PHP or Javascript first. I understand that PHP is server based, does this mean that I will need to provide a server that, for example, their user names/passwords will be stored on? Also, I have been told that Javascript will sometimes be used in PHP, is it necessary that I learn Javascript first?
The only way to secure a login is to have the server do the validation. If you do it on the client, anyone can view the page source and see the code. They can even execute arbitrary javascript code, bypassing any client-side validation.
You will need PHP & some database (MySQL is most common) to create a login system.
You will need to use forms, send the
username and password to a script.
Receive the data and compare the
username & password to the one in
the database
If the login credentials are valid,
create a session variable that keeps
them logged in.
It is worth noting that doing things with only HTTP instead of HTTPS allows hackers (read: Script Kiddies) to hijack your user's sessions if they are on an unsecured network such as open Wi-Fi in Starbucks.
As you say, PHP is server side (executed on a web server), whilst JavaScript is executed on the client side (in the web browser). JavaScript can't be used "in" PHP, but it's often used to improve the user experience on many web sites. However, for the majority of purposes, it can be considered as a separate concern to PHP.
However, let's take a step back. If you think about your problem, you'll probably come to the conclusion that you need to store the information about the users somewhere on the server side, so that you can check the information supplied in the form against the user data you have stored to see if the details are correct. (Once you learn more, you'll most likely decide to store the information in a database, such as the popular MySQL, which PHP can talk to and interrogate using the SQL language.)
However, at this stage of things I'd recommend getting hold of a good book on PHP, or perhaps having a look at the introduction section of the PHP manual, which contains some basic tutorials.
Yikes.
At the minimum you want some sort of server-side language. I'd also highly recommend using a pre-built system, depending upon needs, since security is not easy.
JavaScript is not required.
You will need a host to use for a webserver and for a DB, this can also be your pc check out wamp
Javascript is not required, but using jQuery can certainly help your UI look a lot better. There are a lot of very simple examples of forms (including a login form) inside the jquery site.
Javascript is client-side, it can't auth a user alone, that's were you need PHP. Usually web auth pages don't need javascript, only a client side language, like PHP.
Start with PHP. Javascript is occasionally used for working with PHP on the page (ie., get database info without having to click a "submit" button or navigate to another page.) It is used, for example, to make people's Facebook statuses appear on your homepage in real time. I programmed PHP for years and haven't learned any JS until just recently, so don't worry about it for now.
The posted tutorials (especially on w3) are excellent. There is an excellent tutorial that describes exactly what you are trying to do at devarticles, but it requires a VERY basic understanding of SQL. The example in the tutorial is also fairly unsecure, but it'll teach you the basics of working with MySQL and PHP sessions.
You'll need to run the scripts on a sever that has PHP and MySQL on it, so pay attention to these things when you're looking for hosting.

Simulating Browser Clicks In PHP

I want to write a PHP script that performs a routine task in a web app I use. I am trying to figure out the easiest way to submit a form, click a link, and get some information. What's the easiest way to do this (keeping the session open, etc.).
Javascript would be a better solution than PHP. You can use it in tandem with PHP to submit a form that references the same page, ie. <form method='index.php' action='post'>
If method is GET then you ought to be able to work it out form the URLs of a few real world attempts.
It POST then you are probably SOL unless it's your own web page./app and you know what $_POST it expects ... unless you find a tool to snoop your HTTP traffic and get the POST info from observing a few real wrold examples.
You can use CURL in PHP to simulate submitting data, clicked links, etc., I suppose, but a client-side scripting language like Javascript--as opposed to a server-side language like PHP--is more suited to what you're describing. I'd need more info to give you a specific example.
You will not be able directly emulate those events in PHP as web apps use Javascript on the client side and PHP is a different language and operates on the server side.
Firstly, I would see if there is an open API available for the web app you're wondering about, e.g. Gmail: http://code.google.com/apis/gmail/ . Not all APIs can do what the web app can do, so you'll need to check the documentation to make sure the API does what you want and has an easy way to interface with PHP.
The other option is to essentially reverse engineer how the web app communicates with it's server. Most all web apps operate by sending POST or GET HTTP data in some sort of serialized format like XML, JSON or text. You can use something like the Firebug add-on for Firefox to view POST/GET data. If you know what the server sends to the client and what the client sends to the server, you can essentially write a script using something like CURL to emulate the client in PHP instead of JavaScript. This would take quite a bit of work and probably involves a lot of trail & error.

Categories