Submiting an ajax form with php
i'm building a php program to get 3 variables from a website and calculate them and then post the result, I can read the variables, calculate them, but the posting part is difficult to me because it uses an Ajax form ( I think ) is posting possible with php using XMLhttprequest?
this is the submit button:
<a href="#" class="formSubmit" tabindex="3">
this is the message box:
<textarea name="message" id="messageContent" rows="18" wrap="virtual" tabindex="2"></textarea>
I've tried to handle it like a regular form but it didn't work, I tracked down the ajax function to post:
function formSubmitFunction(e,action, target){
e.stopPropagation();
e.preventDefault();
if ($("#messageContent").attr("value") == undefined || $("#messageContent").attr("value").length < 2){
alert("A mensagem precisa ter no mÃnimo 2 caracteres.");
$("#messageContent"开发者_StackOverflow社区).focus();
return false;
}else {
$("#formSubmit").html('<img src="http://website.com/'+desTheme+'/images/send_post.gif');
if(action == "post"){
$("#formAjax").append('<div id="carregando"></div>');
PostFunctions.insertPost(topicId,$("#messageContent").val(), callbackInsertPost);
} else {
var postId = target.replace("#","");
$("#formAjax").append('<div id="carregando"></div>');
PostFunctions.editPost(postId,$("#messageContent").val(), callbackEditPost);
}
return false;
}
}
help
@edit:
found the other insertPost function
function callbackInsertPost(response){
var result = eval("("+response+")");
if($("#popup").length){
$("#popup").remove();
}
if(!result.error){
var returnMessage = "";
if(result.isForumModerated){
returnMessage = '<div id="popup" class="autoClear simple">'
+'Sua mensagem está aguardando a aprovação da moderação.'
+'</div>';
$("#carregando").remove();
$(actualPostId).prepend(returnMessage);
window.setTimeout(function(){
$("#popup").fadeOut("slow", function(){
$("#popup").remove();
});
},5000);
$("#formAjax").remove();
} else {
//console.log(result);
window.setTimeout(function(){
document.location = "_t_lastpost_"+topicId+"_"+forumId+"?postId="+result.postId;
},1500);
/*returnMessage = '<div id="popup" class="autoClear simple">'
+'Mensagem enviada com sucesso! Clique neste <a href="_t_lastpost_'+topicId+'_'+forumId+'">link</a> para ver sua mensagem'
+'</div>';*/
}
} else {
if(result.nickname_reproved){
document.location = "changenickname.jbb";
} else {
$("#carregando").remove();
for(i = 0; i < result.messages.length; i++){
$("#formAjax").prepend('<div id="popup" class="autoClear error-post">'
+result.messages[i]+'<br/>'
+'</div>');
}
$.scrollTo("#popup",500);
$("#formSubmit").html('<a href="#" class="formSubmit" tabindex="3"><img src="'+baseImages+'/themes/'+desTheme+'/images/pm_send.gif"/></a>');
$(".formSubmit").bind("click",function(e){
formSubmitFunction(e,"post")
});
}
}
}
@edit2:
PostFunctions.insertPost = function(p0, p1, callback) {
DWREngine._execute(PostFunctions._path, 'PostFunctions', 'insertPost', p0, p1, callback);
}
Forget all the JavaScript stuff you are looking at. When emulating the browser, you just have to recreate the requests sent to the server.
- Open up fiddler or firebug's net panel.
- Do the post in your browser.
- Look for the ajax post in fiddler/firebug.
- Create a regular post using PHP curl using the same parameters you saw in fiddler.
Make sure you check the headers in case javascript set some cookies.
EDIT
The cookie file It is created by curl and has a different format than fiddler. Delete it and let curl create it.
Is
c0-id
a hidden form field? View source. If it changes when you look at it in another browser, you need to scrape the form, then do the post you have written based on that value.Post to the page that displays the form. Use
CURL_OPT_COOKIEJAR
.CURL_OPT_COOKIEJAR
it useful if you post to two different pages because it remembers cookies set from the last request, just like a browser.
Make sure your user-agent is a real browser.
Watch both the browser and your script hit the server through fiddler. You want your script to behave exactly as the browser does. So keep tweaking your script to make it closer. Eventually it will work.
Scraping websites can be very frustrating and time consuming at first. Just keep at it.
- Organize your code into functions and classes. It will be easier to do multiple posts this way.
精彩评论