1

This function correctly checks if the iput string is already in use by comparing it to data picked up by PHP from the databese. My problem is that when I pass this variable via Ajax it returns 'NULL' but when I pass other variables they are sent correctly, so the ajax function is working. Maybe something in my function is to blame?

var string, code, codUp, result;
function check(){
      string="<?php echo $html; ?>";
      code = document.getElementById('this_code').value;
      codUp = code.toUpperCase();
      result=string.split(" ");      

if (document.getElementById('this_code').value === "")
{
    return false;
}
else if (result.indexOf(codUp) === -1)
{
    return false;
}
else{alert ('Code already in use.');}
};

AJAX

ajax.open('GET','agencies/gen_ag.php?action=add&name='+name+'codeUp='+codUp+'&nocomp='+nocompt_ag,true);

PHP

if($_GET['action']=='add')  {
$res_seecode = mysql_query("SELECT * FROM agencies WHERE EMP_CODE = '$_GET[code]'");
if(!mysql_num_rows($res_seecode))   {
    $res_add = mysql_query("INSERT INTO agencies VALUES      ('NULL','$_GET[codUp]','$_GET[name]'");

            echo 'Agencie created.';
}

Var_dump(); Returns name but not codUp.

2 Answers 2

2

Look at the query string:

?action=add&name='+name+'codeUp='+codUp'&nocomp='+nocompt_ag

The keys are:

  • action (value is "add")
  • name (value is name variable's value)
  • codeUp (value is codUp variable's value)
  • nocomp (value is nocompat_ag variable's value)

To get the value sent to the server, you use the key with $_GET, $_POST and $_REQUEST.

You should use $_GET["codeUp"], since the key you are using in the query string is "codeUp". codUp is just the Javascript variable that holds the value that is sent to the server for the "codeUp" key.

Sign up to request clarification or add additional context in comments.

Comments

1

As well as making sure the variable names match up (you're still referencing code in one query), you should also URL encode your values...#

ajax.open('GET','agencies/gen_ag.php?action=add&name='+encodeURIComponent(name)+'codeUp='+encodeURIComponent(codUp)'&nocomp='+encodeURIComponent(nocompt_ag),true);

Note the use of encodeURIComponent() - otherwise if your name contained non-url-safe characters like ? or &, it would break things

1 Comment

Thank you for your answer it added usefull informtion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.