Okay this is strange. I'm new to AJAX and I'm trying to use it with php to check a variable from my database before a form is submitted.

It works almost fine. The strange part is that it seems to be saving anything I enter into the text field.

var urlName = $("#url").val();

So we get the var from the text box and hand it over to ajax

$.ajax({ 
                    type: "POST", 
                    url: "check.php", 
                    data: "url="+ urlName+"&pageid="+ pageid, 

Now in check.php I have it echoing $_POST["url"] and then I have the ajax script alerting the msg just to troubleshoot.

If I enter box, it alerts box to me. If I erase box and enter ajax, for example, it will alert box and then alert ajax. If I erase ajax and enter something else it will then alert me each previous attempt...

if $_POST["url"] == the data in the text box then I have no idea why it's caching everything else. Thoughts?

it would probably be helpful to see the rest of the relevant code.

Of course, my bad :D

function urlChecker(){
                var urlName = $("#url").val();
                //alert(urlName);
                var pageid = $("#pageid").val();
                //alert(pageid);

                $("#status").html('<img align="absmiddle" src="images/loader.gif" /> Checking availability...');



                $.ajax({ 
                    type: "POST", 
                    url: "check.php", 
                    data: "url="+ urlName+"&pageid="+ pageid, 
                    success: function(msg){ 

                        $("#status").ajaxComplete(function(event, request, settings){ 
                            alert(msg);
                            if(msg == 'OK')
                            { 
                                //$("#url").removeClass('object_error'); // if necessary
                                $("#url").removeClass("object_error");
                                $(this).html('<img align="absmiddle" src="images/tick.gif" /> OK');
                                //alert("OK");
                                urlCheck = true;

                            } 
                            else if(msg == '1')
                            { 
                                //$("#url").removeClass('object_ok'); // if necessary

                                $(this).html('cms_content is a reserved keyword please choose something else.');
                                //alert("1");
                            }
                            else if(msg == '2')
                            { 
                                //alert("2");
                                $("#url").removeClass("object_error");
                                urlCheck = true;
                            }
                            else{
                                //alert("A page with that url already exists, a number has been added to your entry to preserve uniqueness.");
                                $("#url").val(msg);
                                $("#url").removeClass("object_error");
                                $(this).html('<img align="absmiddle" src="images/tick.gif" />');

                                urlCheck = true;
                            }
                        });
                    }
                });

            }

check.php

<?php
    // This is a code to check the url from a mysql database table
    include("pathpad.php");
    include("ver.php");
    include("adminaccess.php");
    require_once("db.php");
    connect_db();
        //echo("here");

        //echo("Post". $_POST['url']);
        $url = $_POST['url'];
        //echo("Url:".$url);
        $pos = strpos($url, "cms_content");
        if($pos !== false){
            echo '1';
        }

        $pageid = $_POST["pageid"];
        $sql ="SELECT url, pageid FROM tblpage WHERE url=:url";
        $sqlstmt = $_SESSION["db1"]->prepare($sql) or die(mysql_error());
        $sqlstmt->bindParam('url', $url);
        $sqlstmt->execute();
        ////echo($sqlstmt->rowCount());

        if($sqlstmt->rowCount() > 0 || is_dir("../".$url)){
            //echo 'if';
            $rs = $sqlstmt->fetch();
            //echo($rs["pageid"]);
            if($rs["pageid"] == $pageid){
                //echo($pageid);
                echo '2';
            }
            else{
                //echo("else");
                $num = 1;
                $urlNew = $url.$num;
                $loop = true;
                    while($loop == true){
                        //echo("looping");
                        $sql2 ="SELECT url FROM tblpage WHERE url=:url";
                        $sqlstmt2 = $_SESSION["db1"]->prepare($sql2) or die(mysql_error());
                        $sqlstmt2->bindParam('url', $urlNew);
                        $sqlstmt2->execute();


                        if($sqlstmt2->rowCount() > 0 || is_dir("../".$urlNew)){
                            $rs2 = $sqlstmt2->fetch();
                            if($rs2["pageid"] == $pageid){
                                $loop = false;
                                echo '2';
                                break;
                            }
                            else{
                                $urlNew = $url.$num;
                                $num++;
                            }
                        }
                        else{
                            $loop = false;
                            break;
                            echo("false");
                        }
                    }
                echo($urlNew);
            }

        }

        else{
            echo 'OK';
        }

        closeconnect_db();
?>

The logic to this is probably off a bit, I had a stupidly ridiculous time wrapping my head around how to do this when it should have been easy. Sigh.

My main problem is that if I choose a url that exists it adds the number and works fine but if I try to change it after than to something that doesn't exist it continues to insert the modified url instead of keeping the new one.