2
Code:
<script>
$(document).ready(function () {
$("#submitbuy").click(function () {
alert("clicou no submitbuy");
var emailc = $("#emailc").val();
alert("variavel emailc atribuida: " + emailc);
$.ajax({
url: 'https://meusite.com/cadastro.php',
type: 'POST',
data: $("#forms").serialize(),
success: function () {
alert("entrou no success ajax");
if (data.length == 0) {
alert("forms foi submit");
$("#forms").submit();
} // if
else {
alert("forms não foi submit, alert data");
alert(data);
} // else
} // success
}); // ajax
}); // submitbuy.click
}); // document.ready
</script>
What happens to you: Displayed the first two alerts,(I used them as debug) ps: firebug does not point any error in this code above. But I did not display the third alert (the one inside the $.ajax Success).
What seems to be the problem? I have another ajax code on that same page, and it fulfills its role, I’ve gone through every possible mistake, but I found nothing..
EDIT: Form code, to make something more concrete: Form code is like this, it’s pretty simple, just to capture the email:
<form action="https://www.meusite.com/paginax" method="POST" id="forms" name="forms">
<input name="emailc" id="emailc" placeholder="seu melhor email!" id="emailc" type="text">
</form>
the input is out of the form, type button, only to open the function to be later submitted in AJAX.
I think you’re missing an argument here:
success: function(){
... should besuccess: function(data){
. But thisif(data.length==0){
makes a mistake.– Sergio
Indenting the code would help read it...
– Guilherme Nascimento
#submitbuy
is a button<input type="submit"...>
or a link?– Guilherme Nascimento
Sergio, I was missing, I put it again, but it didn’t solve, it must be because I didn’t even get to the Success and that’s why he didn’t point out the bug in the firebug. Guilherme, is a button input,
<input type="button">
and it’s out of the form, because the idea is that the form should not be submitted, but that it should go through this code above when someone clicks onsubmitbuy
, yes later to be submitted using ajax.– Ale
@Alexandrec.Caus I know it should not be submitted, the question is that it could be a flaw and so I asked such a question. See my answer.
– Guilherme Nascimento
@Alexandrec.Caus receives some of the Alerts within the
success
?– Sergio
Sergio, I don’t get paid, so I suspect you’re not entering the Cess, the mistake must be before, but I don’t see..
– Ale
Form code, to make something more concrete: Form code is like this, it’s quite simple, just to capture the email:
<form action="https://www.meusite.com/paginax" method="POST" id="forms" name="forms">
<input name="emailc" id="emailc" placeholder="seu melhor email!" id="emailc" type="text">
</form>
– Ale
you know if ajax is coming to PHP? (
'https://meusite.com/cadastro.php'
)– Sergio
@Alexandrec.Caus As I said in the answer, may be a problem or another, as it is not a problem with
type=submit
, so it’s a problem with the requisition and how you didn’t use iterror:
if an error occurs in the request you will not know, so useerro:
or.fail()
as in the examples I have passed :)– Guilherme Nascimento
I looked at the console (All tab) of Firebug, and came across this, when I clicked on the button
#submitbuy
: Blocked source request: Same source policy prohibits reading remote resources in meusite.com.br/action.php. This can be fixed by moving the resource to the same domain or by activating CORS.&#A; I soon realized that my website URL was with www.meusite.com.br and I was ordering for meusite.com.br/action.php.– Ale
Yes, the www. missing, made the source and destination of the request different, and with that, by default, the server blocked the request giving that error right above. @Guilherme, thank you for the suggestion of
error: function(err) { alert(err) }
, I had not thought of this possibility of catching the error, if it occurred. I also thank the rest who was willing to help me, but as you saw, the error went beyond the code, the server blocked by default this request.– Ale
@Alexandrec.Caus then, I should have seen this but also passed me. Instead of
https://meusite.com/cadastro.php
you need to use relative paths. Switch to/cadastro.php
.– Sergio
@Sergio, true, since if the site is not configured to rewrite the url (I believe it is possible for .htaccess), the user can enter both www., and www, so the best alternative, for both cases, is to use /register.php.
– Ale