Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Get Jquery to check a php response?

Featured Replies

Hi guys, im totally new to php and ajax subsequently... code scares me before i dive into it!

 

However i know my jquery.

 

I aim to make this page: http://www.andyknox.co.uk/free which checks to see if a cookie is present and then displays a certain div to also do this:

 

If the cookie isn't created i.e. the user hasn't joined the mailing list and the cookie isn't present, the submit button needs to execute a code (jquery/ajax?) which checks to see if the users email was successfully added on the php maillist and if successful to store the cookie and then subsequently hide the not_joined div and show the joined_div and if not.. then return with showing an unsuccessful message.

 

How do i do this!? please :) I would love to learn as it would be a nice introduction to web applications and more complex code.

 

Thanks

Stefan

You can use the the jQuery ajax method. Which will allow a few parameters one of which being url.

 

In the url you pass in your PHP script.

 

You could do all of the hard work in PHP and keep the jQuery simple for this. I think ajax() needs 4 parameters at minimum. Check jquery.com for full details.

 

In your PHP script just check if the cookie is set and if it was echo the div to the page. If not then return false on the script.

 

I am told doing anything but returning something within a function is suicide but for something so smallscale i see no troubles with it.

 

jQuery will then return the script in the target element you specified. e,g, div#form

Ah, in that case you need to find out what cookie mail list sets for this and perform the PHP on that.

 

It would be something like

 

if(!$_COOKIE['signed-up'])
{
echo '<div id="this">Your stuff here</div>';
}
else
{
return false;
}

  • Author

This is my ajax i have set up using internet resources... not actually sure what it alll means, but got a fair idea!...

 

<script type="text/javascript">

var xmlhttp;
function ajaxget()
{
xmlhttp=null;
if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
 }
else
 {// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
xmlhttp.onreadystatechange=state_Change;
var addressvalue=encodeURIComponent(document.getElementById("address").value)
xmlhttp.open("GET", "http://www.andyknox.co.uk/maillist/index.php?address="+addressvalue+"&Submit=Submit", true)
xmlhttp.send(null)
}

function state_Change(){
if (xmlhttp.readyState==4)
 {// 4 = "loaded"
 if (xmlhttp.status==200)
   {// 200 = "OK"

	alert(xmlhttp.responseText)
	if ($.cookie("mp3Joined") == "joined"){
   // make some jQuery call
$('.loading').hide();
$('#joined').show('slow')
 }else{
   // "stop"
$('.loading').hide();
$('#not_joined').show('slow')
 }


}
else{
alert("There was a problem with your request")
}
}
}
</script>

 

and I added a line in the success response of my mailing list php to store the cookie with the correct variables.

 

However i just keep getting the error on the ajaax request.. check it out here http://www.andyknox.co.uk/free

 

Any ideas thanks!!!

Stefan

hidden.php file

// set your cookie with JQ if they are not subscribed
// again you need to get a call back from mailing list to do this.
// PHP would be preffered
if($_COOKIE['signed-up-to-mailing-list'])
{
$yourHTML = 'Already signed up';
}
else
{
$setCookieWithJquery = '$.cookie("signed-up-to-mailing-list", 1)';
}

 

index.php *currently index.html

$.ajax({
type: "POST",
url: "hidden.php",
// your email input
data: $("input#email").val(),
// JQ does all the hard work, will grab any results from PHP
// and place it in the responseText i think that's your xmlhttprequest thingy
success: function(msg){
// This div set to display: none; 
$("#div").append(msg).slideDown("normal");
}
});

 

Let me know how you go, not tested this.

  • Author

Still no luck! heres my jquery/ajax:

 

function ajaxGet(){

var address = $("#address").val();


$.ajax({
type: "GET",
url: "http://www.andyknox.co.uk/maillist/inc/storeAddress2.php",
data:"address="+address,
success: function(msg){
//if statement to check the cookie exits that is created when e-mail address was successfully submitted and was valid
	if ($.cookie("mp3Joined") == "joined"){
   // make some jQuery call
$('#.loading').text('').append(msg).show();
$('#joined').show('slow');
 }else{
   // "stop"
$('.loading').text('').append(msg).show();
$('#not_joined').show('slow');
 }
}
});

}
</script>

 

The loading div is hidden hence the .show()

 

and my php file:

<?php
function storeAddress() {
 $message = " ";
 // Check for an email address in the query string
 if( !isset($_GET['address']) ){
   // No email address provided
 }
 else {
   // Get email address from the query string
   $address = $_GET['address'];
   // Validate Address
   if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $address)) {
     echo '<strong>Error</strong>: An invalid email address was provided.';
   }
   else {
$key = $address;
//load file into $fc array
$fc=file("maillist.php");
//open same file and use "w" to clear file
$f=fopen("maillist.php","w");
//loop through array using foreach
foreach($fc as $line)
{
    if (!strstr($line,$key)) //look for $key in each line
          fputs($f,$line); //place $line back in file
}
fclose($f);
$myFile = "maillist.php";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, $address);
$stringData = "<br>\n";
fwrite($fh, $stringData);
fclose($fh);

     if(mysql_error()){
       echo '<strong>Error</strong>: There was an error storing your email address, try again later.';
     }
     else {
	  setcookie("mp3Joined", "joined");
       echo 'Thanks for signing up!';
     }
   }
 }

}
?>

 

 

Please help me it doesnt even show a response?

 

Stefan

  • Author

Hey just to let you know I got there in the end! Good learning curve. I learnt a lot more about jquery, php and cookies :)

 

Thanks

 

Stefan

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.