Simple Javascript question

You need to fill out an e-mail and submit.

The Javascript with an error is set to not run until you submit the form.

There is an unexpected token? Here is the updated code, ryan

[code]$(“#subscribers”).submit(function(event) {
event.preventDefault();

$("body").append("<div class=\"overlay\"><div class=\"loading-bar\"><span></span><span></span><span></span><span></span><span></span></div></div>");
$(".overlay").css("z-index","9999");
var email=$(".enter-information").val();
$(".enter-information").prop("disabled", true);
$.ajax({
  type: "POST",
  dataType: "text",
  data: {
    'subEmail': email
  },
  url: "scripts/process.php",
  success: function(data) {
    data = JSON.parse(data);
    if(data.result[0])
    {
      $(".overlay").remove();
      $("#subscribers").fadeTo(1000,0);
      $("<p class=\"success-message\"><span>Success:</span> Thank you for subscribing. Please check your e-mail, <strong>"+data.result[1]+"</strong>, for the confirmation code.</p>").appendTo("#newsletter").delay(1500).fadeIn(1000);
    }
    else
    {
      $(".overlay").remove();
      $(".enter-information").prop("disabled", false);
      if(data.result[1]==="doubleentry")
      {
        $("<span id=\"attempts-error\" class=\"error\">Error: You are already subscribed</span>").appendTo("#subscribers");
      }
      else if(data.result[1]==="format")
      {
        $("<span class=\"error\">Error: Please enter a valid e-mail address</span>").appendTo("#subscribers");
      }
      $(".enter-information").removeClass("valid").addClass("error");
    }
  }
});

return false;
});

[/code]

BTW, what is json?

The console is telling me that line 16 is the issue, which is the data=JSON.parse(data).

What’s your process.php?

I highly doubt you changed your HTML to match that data I had in my Javascript. This is likely faulty data.

JavaScript Object Notation.

It is a common way of defining objects and properties in JavaScript that has also turned out to be a useful way to pass data between programming languages.

1 Like

Yup I was right. Your name is still name=“Email”. Needs to have that updated. You updated the ID but that’s not what PHP looks at. It looks at the name attribute when it looks at $_POST[‘whatever’]. That “whatever” is whatever the name attribute holds.

Process.php is [code]if(isset($_POST[“subEmail”]))
{
$email=filter_input(INPUT_POST,“subEmail”);
$formData=new Subscribers($pdo);
$isValid=$formData->newSubscriber($email);
}
$isAjax = !empty($_SERVER[‘HTTP_X_REQUESTED_WITH’]) && strtolower($_SERVER[‘HTTP_X_REQUESTED_WITH’]) == ‘xmlhttprequest’;
if($isAjax)
{
$result=[“result” => $isValid];
header(“Content-Type: application/json”);
exit(json_encode($result));
}

if(isset($_POST[“subscribe”]))
{
if($isValid[0])
header(“Location: http://www.reesecodes.come/index?result=subscribed&email=“.$isValid[1].”#newsletter”);
else
header(“Location: http://www.reesecodes.com/index?error=$isValid[1]#newsletter”);
}

[/code]

so in post I have to change that value to Email?

Just change your HTML name to subEmail.

to what though, I’m not sure what you’re talking about. Change name attribute of the input for email to subEmail?

Yes. That name=“subEmail” must be there. Not name=“Email”. That “Email” name is doing absolutely nothing right now. That name attribute must match the data that is being passed via jQuery.

And that must match the $_POST[‘subEmail’] that is being sent. Easier to just update the HTML rather than the JS/PHP.

Ok, we got progress. Doesn’t seem to be finishing out the code, you will see when you try it. I used your css file for now ryan. Also, fordata: { 'subEmail': email }, in the javascript. This doesn’t seem right, are you sure that’s what it is supposed to be?

If that’s what you are meaning to send along

Can you please remove that custom overlay stuff from my code? Really hampering from me being able to do stuff. Remove all aesthetics. We just need the base code working.

Also your PHP is just copy/pasted from mine. How do you expect it to return anything?! Your error logs on your server are probably filled with errors from faulty PHP.

I put in comments the entire contents of your miscellaneous css

I’ve already mentioned this is custom class/method code by me. You cannot possibly make use of it.

Okay, so do I delete that entire if statement and the bracket/ Your previous post of removing the if statement and closing brackets were aimed at the javascript originally

No, you remove those two lines. Just to get this working, we are going to assume all input is going to be true/valid.

So replace those two lines with …

$isValid=array(true, "");

It’ll do for now.

FYI, in your PHP, you’ll need to do something like this for false values (the second value I make there for the error message)

$isValid=array(false, "errorhere");

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.