Instead of using code like this:
'<td> Cancel
I would like to be able to simply call a function and pass the $memberId and $classId into that instead of having to create a new PHP page to run some code with these values.
So basically the user will click the 'cancel' button and then it will run a function from another class, instead of clicking the button and then having another page deal with the request.
How would I go about doing that?
No! You have to use JavaScript for that. PHP is a server-side language, and PHP process died when you see something in the browser page.
The only way for HTML page loaded in a browser and a PHP program hosted on a web server to interact is through HTTP requests.
You can't call a PHP function from a web page. You can only make an HTTP request (e.g. through a link, a form submission or XMLHttpRequest) which sends some data to a PHP program. That program can then look at the data (e.g. via $_GET) and use that to determine which function to call.
Related
I have two separate processes and I am wondering how I can combine them.
I have a PHP email sign-up form which posts the users email address and other data into an SQL DB.
I have a sync PHP script API which once run adds all info from the SQL DB to a 3rd party site (mailchimp)
How can I combine them so once a new user adds their details, and the form is submitted the PHP API script runs. I did this, however it runs the script and the user has to wait until the API call is done:
$appUrl = $_SERVER['HTTP_HOST'];
$path = 'newsletter/mailchimp.php';//your path here
$appUrl = 'http://'.$appUrl.'/'.$path;
if (count($_POST)>0) echo file_get_contents($appUrl);?>
Find an HTML-element which you can bind your function to, like your form, button, checkbox or whatever. There's many different ways like $("form").submit(function(){});, .click, .changed. After that you can use the $.post() function to call the PHP-files.
$(document).ready(function(){
$("#buttonId").click(function(){
$.post('phpfile1.php');
$.post('phpfile2.php');
});
});
If you're getting the content from a remote site, then you can't send it to the client before you've got it.
But in your example you are calling a URL on the local machine - if you include the file instead of calling it in a new HTTP request then you'll get a small improvement in speed.
If you don't need the content from the other URL, then call it from a shutdown function after your script generates the content flushes its buffers and exits.
Calling the second url via javascript is ot a good idea if the transaction spans both codesets - you're putting the user in control of the flow in your application.
Okay so I am working on a simple social network. I am building it with PHP and MySQL. Users can post status updates just like on Facebook. I know how to use the INSERT INTO and SELECT FROM statements to input/output user statuses, but how would I go about having a button (an X) that when clicked it calls a function that holds a MySQL query that DELETES the post? Or one that edits the post? Is the way to do it a call to a function()?
PHP runs on the server. The button exists on the client. So you can't call a function directly.
You need to have a script that accepts some input over HTTP (since you want to do something, this should be an HTTP POST request, in PHP you can read that data via $_POST[]). The script should sanity check the input, do any appropriate actions based on that input and make a response indicating success or failure.
The simplest way to have the browser make the request is to use a <form>. This also allows you to collect information you may need from the user.
To do it "just like on Facebook" (while following best practises) you should use JavaScript to bind a submit event listener to the form, then stop the default event and use XMLHttpRequest to make the HTTP POST request instead (this is Ajax). Then you use DOM manipulation to update the information displayed to the user.
You need to learn AJAX to do that. With an Ajax call, you would capture the click event with JavaScript, and will post an AJAX request to a PHP script that will issue the corresponding delete sql statement to drop the post.
I've got the following problem, I send some value from jQuery to PHP via AJAX. My PHP script receives those value, and I would like that this script prints values in iframe in my www site. But instead, the script response contains all its source code to AJAX as response to the alert message. Does anybody know how can I stop returning code to AJAX and execute it by returning the values to be used in my iframe?
EDIT:
further details: the problem is that my application has got two iframes from different servers, and I need to send values from one iframe to another. Because of cross domain restrictions, I cant do this via JS directly, so I figured out to send values via AJAX from one iframe to PHP on first server and PHP from first server to PHP on another server which is in iframe on my main page, and then show the data. so I must use AJAX to send it.
You can't. PHP doesn't know about the iframe.
You are, presumably, using XHR to make the request, therefore XHR will receive the response.
If you want to load a document into an iframe, then load a document into the iframe (e.g. by setting its src to the URI for the document), don't play around with Ajax.
in php i'm using GET to create query string which launches an action :
for example when i go to :
www.example.com/index.php?mode=action&name=launch_this
it will get mode=action and name=launch_this and the php will launch a function called "launch_this"
But how can i avoid people from refreshing this page and then re-launch the process ?
i'd like to be able to get these parameters once and convert the url for the client to index.php without parameters..
Is it possible ?
Thanks
actions which modify state on the server or launch a process should not use GET, but POST. If you use POST and the user refreshes, the browser will at least prompt for confirmation.
To avoid refreshes, you should send a redirect once the process is launched :
user posts to launch action
server launches action and sends a redirect
user browser receives the redirect and GETs the page atthe given URL
user refreshes, and reloads the page, but doesn't relaunch the action.
This pattern is known as Post/redirect/get, or as "redirect after post".
You can use the Post/Redirect/Get pattern for this:
http://en.wikipedia.org/wiki/Post/Redirect/Get
I'd consider looking at the Post/Redirect/Get pattern.
Before you send any output to the client, set the redirect header.
header("Location: index.php");
This will cause the browser to load the index.php page, and the URL shown in the location bar will change too.
You could also use the history.replaceState() javascript function to change the URL in the browser, but this function is only supported in Safari and Firefox 4 (see for example https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history)
You could store some value inside $_SESSION and check it before execution like
if(!isset($_SESSION['launched'])) {
$_SESSION['launched'] = true;
...
...
}
POST/Redirect/GET is a great answer.
Another possibility would be to use AJAX to actually send the data to the server, and then simply refresh the current page (or navigate to a different one, for that matter) once the AJAX completes. This way, the request that actually sends the data to the server is only ever called upon user action, and would not be called on reload.
I'm trying to figure out if I can do the following:
User submits form to script via jquery post
Depending on what was processed, script may or may not return errors
If the script did not process any errors (i.e. SUCCESS), I want to redirect to a SUCCESS page
I know it's possible to redirect the browser via javascript, but I'm worried if some users have javascript disabled the entire flow might get messed up.
Thanks in advance.
As you want to evaluate the form (currently) with jQuery, you can safely assume that, when the form is evaluated, JavaScript is available.
For those without JavaScript, you should add some server side routine, that does the job. And then you can use a redirect via HTTP header, or a <meta> redirect.
To keep both ways working, hard code the server side routine into the code, and replace it via jQuery when the page is loaded. JavaScript users will execute the jQuery replacement and get the jQuery evaluation; other's will just stay with the normal version.