سؤال

I'm currently stuck at my "Newsletter Subscribe Form". I use MailChimp to send my newsletters and have managed to redirect the visitor of my page to a custom PHP file after entering the email.

The process goes like this:

  1. User enter email
  2. Email silently (user doesn't see it) gets added to the MailChimp Database (Email List)
  3. Instant redirection to a self hosted PHP script.

4. The PHP Script changes the color of the button via a CSS Class in the HTML file.

I'm stuck right at Point 4, since I don't really know how to change a CSS Class in a HTML file with PHP. It's important that the webpage still remains in HTML (otherwise i'd use a simple variable in PHP). Do I need to parse the PHP value to AJAX or JSON which then changes the class (probably with jquery?)

If yes, could you give me an example on how to do it? I have never really used JSON or AJAX before.

Thank you guys very much :)

هل كانت مفيدة؟

المحلول

It's a little bit difficult to understand the question completely. If you're trying to output a class when the page loads, that's very easy using php:

<?php
$classVariable = "myClass";
?>
<div id="someID" class="<?php echo $classVariable; ?>">Some content</div>

PHP loads once, before anything else on the page, and never runs again. You can call other scripts using AJAX which can then return data that you can use. For example (using jquery)

<script type="text/javascript">
$.get("path-to-myscript.php", function(response){
   // read the response in, perhaps you grab some class name from it
   // for demo purposes, lets say this response is in json formatted as a simple string:
   // '{ "class" : "someClass" }'
   var data = $.parseJSON(response);
   var newClass = data.class;
   $("#someID").attr("class", newClass);
});
</script>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top