I'm trying to track conversions on a button that is triggered by AJAX with visualwebsiteoptimizer's Custom Conversion Goal Code which is generated javascript code.
If I put it in php file that is being triggered by AJAX would it run this javascript code aswell sending details back to the visualwebsiteoptimizer?
Or is there a better way of doing this? (ie onclick event on the submit button...)
You are not going to be able to run javascript from inside your PHP script. Javascript needs to be executed by the browser (at least in this case).
Your best bet is to run the custom conversion code in an onclick (or similar) event. You may want to look into using a $('form').submit() event or the callback function for your ajax request.
Related
I want to execute sql command such as
$query_run = mysql_query($query);
in php. But the only way I can find to trigger php event is to submit a form using:
if(isset$POST['submit']) {
$query_run = mysql_query($query);
}
But this would refresh the page and wipe off every thing that dynamically created on the page.
What I want is to have a button, press which to trigger sql commands without reloading the page like onclick in js. However, sql command cannot be run inside js function (as I can find).
PHP is parsed server side which means you can't have it run code without making a server request. Once the page is delivered to the client any php parsing is complete.
You can create a button or link that will send off an http request through javascript, an AJAX call, to a small php script that does the database work you want done. You wouldn't have it call your whole page again. The output from this gets returned to the javascript and can be used to update a CSS block element with success or failure notices.
Here's the W3C Ajax tutorial http://www.w3schools.com/ajax/default.asp
What you need to do is an AJAX call. This requires Javascript (preferably with the help of a library such as jQuery) to assemble the date from the page, make the call to the PHP page, and to present the results onto the page.
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.
Is there a way to trigger a function when a $_POST is received?
I have a php page that finishes executing and calls another page. The other page performs certain actions and POST updates back to this page. I need to be able to update a div as and when a POST is received.
Making an ajax request and using "success" callback as a trigger doesn't work since this will update my div ONLY ONCE. The problem is that this page will receive POST multiple times at irregular intervals and I need a way to trigger an action whenever POST is received.
Use Ajax (I'd use jQuery) and set a time interval to check if there was a status change. The "other page" should not post back to the first page, but save data in a "requestable" area (might be an database, a file etc) where this new info/status will be stored and retrieved periodically by the Ajax request.
Use AJAX, because you can't trigger $_POST recieving in php,
As I understand you just need to update some <div> inner html after post is recieved, and you are able to do it with jQuery events
I would do this in the following way (below is the PHP code, just FYI):
if (!empty($_POST)) {
// do whatever you mean by 'updating the div'
}
However AJAX may be more appropriate and it is (believe or not) more flexible.
AJAX call will be smaller load on your server and can update the box single or multiple times (depending on how you write the JS code) - in such case I recommend using jQuery for simplicity (if you are not familiar with JS).
usually, I would do something like this...
$var_posted=0;
if($_POST['var']){$var_posted=1;}
then...
if($var_posted==1){some_function();}
I know how to work with fields and buttons in a form.
But how do i use this kinda code http://www.useragentman.com/tests/dragAndDrop/permissionForm.html
and use its values with html or php.
Basically, how do tell what name has put in which field in code.
Thanks.
This will require a significant amount of JavaScript AJAX code to interface with PHP. When an item is dropped onto a target, a JavaScript function bound to the item's drop event is called. That function must then initiate an AJAX call which passes the contents of the dragged item and an identifier of the target box (where it was dropped) up to a PHP script on the server.
The companion page has a few code examples for handling the javascript. But you will need to read up quite a bit on AJAX before attempting to use this with PHP.
can any one please help how to get the values from the javascript to php other than while using submit button.
scenario:
i am searching for record, if the record found then, i need confrim alert asking to continue or not, if he click continue how can i say he selected continue
If you want to check without having a page reload, you probably want to execute an AJAX call, then depending on the result returned by the underlying PHP script, take the appropriate action. If you have no knowldege of how to implement this, take a look here
You can never use JavaScript to communicate with the page while it is loading, you can only send a new request to the web server from the JavaScript layer... although you can send that request to the same script that's already running, it will still be a new instance of the PHP script, just like when you open a new browser tab to the same page.
The only way for JavaScript to communicate with PHP at all, is by sending an HTTP request. But you don't have to refresh the page in order to do that if you use AJAX.
AJAX is basically a word to describe JavaScript exchanging information with web pages without refreshing the page. But note that it will still not be able to change variables in the PHP script which is running when the JavaScript code is executed.
In the case of PHP, I've used the open-source library SAJAX which is quite simple. You will find it at http://www.modernmethod.com/sajax/
Hope it helps and good luck!
You can use this as an example using jquery and PHP:
$.post('searchimage.php', { action: 'searchimage', imgreference: $(this).val() },
function(data) {imgsample.html(data);}
);
Basically apply the above function in a document ready function so its run when the page loads.
This can be triggered using $("#prodcode").click() or what ever event handler you want to use.
The php page in my example will get sent the value from imgreference as a post, you can do whatever you want in the php page then return the value which gets added to the imgsample (in this case a td)
hope this helps.