Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-20-2012, 05:31 PM   PM User | #1
paddyfields
Regular Coder

 
Join Date: Dec 2010
Location: London
Posts: 283
Thanks: 60
Thanked 11 Times in 11 Posts
paddyfields is an unknown quantity at this point
Question Possible function problem, data not entering.

Hello.

I'm really really stuck on this and can't see where I'm going wrong. The idea is you click the button 'save search' and it enters two bits of information.. 'memberid' and 'location' into the database, via AJAX and displays a 'saved!' message after it's clicked.

I've debugged it as much as i know how.. echoing the $location and $memberid[0] which both show up correctly, and also testing the php address with the values just typed in, and it enters into the database correctly (ie http://mywebsite.com/savesearch_save...camden&mem=165 directly into the browser address).

Can anyone see what I've missed? At the moment you just click the button and nothing happens.

Cheers
Pat.

HTML
Code:
<div id="saveSearch"><button onclick="ajaxSaveSearch(true,<?php echo $location;?>,<?php echo $memberid[0];?>)">save search</button></div>
JAVASCRIPT
Code:
function ajaxSaveSearch(doSave, location, memberid) {
    var ajaxRequest; // The variable that makes Ajax possible!
    try {
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e) {
        // Internet Explorer Browsers
        try {
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function () {
        var div = document.getElementById("saveSearch");
        if (ajaxRequest.readyState == 4) {
            div.innerHTML = ajaxRequest.responseText;
        } else {
            div.innerHTML = "<img src='Images/icons/loading.gif' />";
        }
    }
    var queryString = "?location=" + location + "&mem=" + memberid;
    var url = doSave ? "savesearch_save.php" : "savesearch_forget.php";
    ajaxRequest.open("GET", url + queryString, true);
    ajaxRequest.send(null);
}
savesearch_save.php
PHP Code:
include("connect.php");

$location $_GET['location'];
$themember $_GET['mem'];

$sql "INSERT IGNORE INTO savedsearch (memberid, location) VALUES ('$themember','$location')";
if (!
mysql_query($sql$con)) {
    die(
'Error: '.mysql_error());
}
echo 
"saved!"

Last edited by paddyfields; 11-20-2012 at 05:39 PM..
paddyfields is offline   Reply With Quote
Old 11-20-2012, 06:07 PM   PM User | #2
paddyfields
Regular Coder

 
Join Date: Dec 2010
Location: London
Posts: 283
Thanks: 60
Thanked 11 Times in 11 Posts
paddyfields is an unknown quantity at this point
solved it... should have concentrated on the html

ajaxSaveSearch('location',memberid)
paddyfields is offline   Reply With Quote
Old 11-20-2012, 06:44 PM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,532
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
That's one reason why you shouldn't jumble JavaScript with HTML and should keep all the JavaScript in a separate file instead.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:32 PM.


Advertisement
Log in to turn off these ads.