Tell Controller On Another Domain There Is New Data - php

Currently I have two sites hosted on different domains.
SITE 1: I want to use as a CMS for content creation only.
(writing,editing,etc.)
SITE 2: I want to use for viewing content.
Theoretically, what I'd like to be able to do, is create multiple peices of content in SITE 1.
When I'm ready to publish these I'll press a 'publish' button, which will alert SITE 2 that there is new content and pass the values via JSONP.
SITE 2 will then pull in the data via JSON and do what it wants...(enter into DB, etc.)
...so yeah I'm stuck at the theoretical part. I'm not exactly sure where to go now but here's what I'm thinking.
There's a javascript function on SITE 1 that is called when I choose publish:
$.ajax({
type: "POST",
dataType: "jsonp",
data: postData,
url: 'http://site2.com/admin_json_controller.php',
success: function(data) {
// 'data' is a JSON object which we can access directly.
// Evaluate the data.success member and do something appropriate...
if (data.success == true){
alert('worked!');
}
else{
alert('did not work!');
}
}
});
This posts a json object to a php file on SITE 2. This file will simply be waiting for this post object. When it recognizes the post object...it will then enter the values into the database.
Does this make sense? I have a feeling I don't quite understand JSON yet but any help, questions, tips, pointers are much appreciated.
THANKS

To simplify things you have two options:
Use the same database for both domains.
Do a simple post to the other domain. Set the action of the form to script on the other domain. The other domain can access the variables via $_POST.

Related

Why my AJAX function blocks the header of PHP that I have on my page?

I am doing a program in PHP (MVC) in which I need to delete a row from the database when I click on a link on the View side. So, when I click on the link, the following ajax function it is called.
var deleteCar = function(id)
{
$.ajax({
type: "POST",
url: "http://localhost/project/car/deleteCar/" + id,
success: function(response){
}
});
}
but I do not want to send any data so it is the reason why I put it as above.
Then, in the Controller side I have the following method:
public function deleteCar($id)
{
//Here I call the function to delete the Car that I send by id. It works fine.
header('Location: http://localhost/project/car');
}
If I call directly the method deleteCar on the link without Ajax the header works properly but in the same moment I use Ajax to call it, I have to refresh the page to see the content that I have modified, I mean, that the Car have been deleted.
The code works fine, just I do not want to refresh the page after AJAX function had finished.
Thanks in advance!
I am guessing the use case is to allow the app to work when the user does not have JS enabled - so they just click the links and get a non-AJAX experience. In this case you probably want to redirect ONLY if the page was requested via GET, not POST. something like
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
header('Location: http://localhost/project/car');
}
is likely what you are looking for.
You will then have to actually remove the element representing the car from the DOM in your success handler, with something like:
var deleteCar = function(id)
{
$.ajax({
type: "POST",
url: "http://localhost/project/car/deleteCar/" + id,
success: function(response){
$('#car-row-' + id).remove();
}
});
}
(that won't be it exactly, it depends how the HTML of your page is setup how exactly you will do this).
I believe the key thing to understand here is - when your PHP function has completed it has removed the car from the database, but the browser still has the same HTML it got from the page originally. Just sending an AJAX request won't change that. If you want the HTML in the browser to change to reflect the new state of the database, you will NEED to do one of two things:
Refresh the page, so the entire thing is rebuilt by PHP based on the current database state
Use javascript to change the HTML in the browser, to reflect the changes you have made to the database state.
It is wrong on so many levels but it's difficult to put in words. It's subtle.
Long story short - think about jquery.ajax as of another virtual tab of you browser.
When you make ajax-request to the server - you create new virtual tab.
You php header could affect this virtual tab and redirect it where that header defined.
But it will redirect that virtual tab, not your current tab - if that makes sense.
What are your options? On success - make redirect with pure javascript.
success: function(response){
location.href = "http://localhost/project/car";
}
This would be the basic way to solve your problem.

SEO friendly url creation using js, php in html page

I am developing a website for a client using html, js and php. For any database call i am using ajax call
$.ajax({
type: 'POST',
url: "index2.php",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg, status) {
dosomething(msg);
},
error: function (msg, status) {
console.log("failure");
console.log(msg);
}
});
and php pages returns me the output. The problem is that index.html page url is not getting changed.....I want it to change say index.html?id=23 (creating query string in html page)
Basically my aim to make it SEO friendly url like index.html/helloppl
What is the best method to do this. While googling i found link for doing....still i need to create query string here...any help is appreciated.
Creating SEO fridenly url.
Creating query string in html
The problem is that index.html page url is not getting changed. I want it to change say index.html?id=23 (creating query string in html page)
Ans: if you want to change the url like you have mention above this the ajax will not the way to do this . This can be done in simple way go to the url i have provided and look at the topic.
http://html.net/tutorials/php/lesson10.php
Basically my aim to make it SEO friendly url like index.html/helloppl. What is the best method to do this. While googling i found link for doing this. I still need to create query string here.
Ans: This can be done with htaccess. Go To The Link
Jquery ajax() messing with my .htaccess mod_rewrite

rebuild website for web 2.0

I want to rebuild my own old ASP classic website to new standarts of web2.0 in PHP (as I understand it). On my main website page there are some areas that must show last updated things like: last threads on forum, last news, last talkbacks and etc. For now (on ASP version) I have all data loaded from DB to Application (memory) and page is reloaded every 4 minuts and each time taking data from Application. If data was changed (new talkback was added for example) Application object is set to null and data reloaded from DB.
My question is about best practices in web2.0: how should I make these areas been refreshed? I need to find the way to refresh only some parts of page. I thought about 2 ways:
1. put (again) data in memory, put iframes on page and reload them.
2. create data in XML file on server and load it from there each 4 minutes, when new thing added (like new talkback) recreate the XML file.
Is there some best practicies for solving such things?
You can replace the content of some divs with a html code got from an ajax request. For example:
<script type="text/javascript">
setInserval(function() {
$.ajax({
url: "response.php", // Page url
data: "querystring=1&ciao=2", // Your querystring
type: "POST", // Request type
dataType: "html", // Expected result
success: function(data) { // On success
$("#id_div").html(data); // Replace the content of #id_div with the response
}
}, 240000);
</script>
The page response.php must return the html to replace and require jQuery.

how to block or protect ajax post values from browser inspect element

function updateR()
{
$.ajax({
type: "POST",
url: "update.php",
data: { surprize: '12345678910', dpoint: point, dlevel: level }
}).done(function( msg ) {
// alert( "Data Saved: " + msg );
});
}
it my function . how to block or protect ajax post values (point or level) from browser inspect element if user want open browser inspect element and change value point ?
In short, you cant!. Yes you can do a bunch of stuff to hide it in some way or add some garbage to obfuscate it but in the end a determined user will find it. Quite simply because the user can see the code that is responsible for sending this to the server.
Now you can make it difficult for someone trying to this, by maybe adding junk values to the AJAX request, or by appending the actual data with some number of junk values. But then you have to obfuscate your javascript code which is responsible for doing this too.
But dont dont dont dont ever rely on this! Because you can only make it difficult but someone determined will be able to do it. And if you find yourself in a situation where you need to send some data to the server that is confidential to the user, then you need to re think your system architecture instead of trying to hide the request from the user.
A web application is a client server application, you can never trust data that is coming from the client, maybe you could do it harder to cheat, but the user always has the possibility to send modified data to your server.
Actually, user can send whatever he wants, if its through browser or just command line / any other tool.
You have to sanitize & filter your input on your server side.
For ex. if you know that you want only numeric values for post var named 'surprize', you have to validate it by the following way:
if(empty($_POST['surprize'])||!is_numeric($_POST['surprize'])){
//invalid surprize
die('Bad Surprize value');
}

jquery $.ajax request remains pending

I have made a simple chat application which uses long-polling approach using jquery,
function sendchat(){
// this code sends the message
$.ajax({
url: "send.php",
async: true,
data: { /* send inputbox1.value */ },
success: function(data) { }
});
}
function listen_for_message(){
// this code listens for message
$.ajax({
url: "listen.php",
async: true,
timeout:5000,
success: function(data) { // the message recievend so display that message and make new request for listening new messages
$('#display').html(data);
listen_for_message();
}
});
}
THIS SHOULD HAPPEN : after page loaded the infinite request for listen.php occurs and when user sends message, the code sends message to database via send.php.
PROBLEM is, using firebug i've found that send.php request which is performed after listen.php request, is remains pending. means the request for send message is remains pending.
The issue was because of session locking;
both send.php and listen.php files use session variables,
so session is locked in listen.php file and the other file (here send.php file) can't be served after the session frees from serving another file ( here listen.php).
How do I implement basic "Long Polling"?
the link above is a similar question that may help you.
it does not have to be on a database, it can be saved on a tmp file, but your problem is that you are choking the browser by performing too many requests, any one browser handles two requests at a time, which means you should really allow the browser to finish the first requests first then do the second one... and so on...
you do not need to do send.php and listen.php, because you can do it simply on one page both of them.
function check(){
$.ajax({
url : 'process.php',
data : {msg:'blabla'/* add data here to post e.g inputbox1.value or serialised data */}
type : 'post',
success: function (r){
if(r.message){
$('#result').append(r.message);
check();//can use a setTimeout here if you wish
}
}
});
}
process.php
<?php
$msg = $_POST['msg'];//is blabla in this case.
$arg['message'] = $msg;//or grab from db or file
//obviously you will have to put it on a database or on a file ... your choice
//so you can differentiate who sent what to whom.
echo json_encode($arg);
?>
obviously this are only guide lines, but you will exhaust your bandwidth with this method, however it will be alot better because you have only one small file that returns either 0 to 1 byte of information, or more if there is a message posted.
I have not tested this so don't rely on it to work straight away you need a bit of changes to make it work but just helps you understand how you should do it.
however if you are looking for long pulling ajax there are loads of scripts out there already made and fine tuned and have been test, bug fixed and many minds help built it, my advice is don't re-invent the wheel

Categories