Jump to content

Can Anyone See My Problem?


fry2010

Recommended Posts

I have a form that im checking each entry with using ajax, before the user can submit the form.

Im sending it with post method. The problem is in my response function when I try to compare the values of the string that the php script passes to it, to a string that I predetermine.

 

For example my php script is returning a string 'user_ok'. Now it is actually doing this because I outputted this with an alert(str);

But when I try to output the alert(str) inside a comparison function as such:

 

if(str == 'user_ok')

{

  alert(str);

}

 

it does not work even though it is actually passing back the string 'user_ok'.

 

Here is my php script:

  $name = $_POST['name'];

  $resp = '';

  if(isset($name))
  {
    if(valid_name($name) && strlen($name) < 30)
    {
      $resp = 'name_ok';
    }
  }

      header('Content-Type: text/plain');
      print $resp;

 

and here is the response function in my js file:

 

  this.response = function(str)
  {
    var self = reg;

    if(str == 'name_ok')
    {
      alert(str);
    }
}

 

when I do the following in my javascript file it outputs the correct string 'user_ok':

 

  this.response = function(str)
  {
    var self = reg;

    alert(str);
    if(str == 'name_ok')
    {
       // do stuff here
    }
}

 

so the problem appears to be with my if() comparison....

 

any help REALLY appreciated....

 

 

Link to comment
Share on other sites

Ok so there is deffinatly somthing wrong with my if() statement.

 

I have just put an else statement after the if to alert(str), and it still outputs the alert even though the string value is what is in the if() statement. here it is:

 

  this.response = function(str)
  {
    var self = reg;

    if(str == 'name_ok')
    {
       //do nothing
    }
    else
    {
      alert(str);
    }
}

 

so even though the output from the alert says 'name_ok' it shouldnt have done so because there is nothing in the if() statement.

 

Could it be something to do with how the string is being passed back to it? maybe its not behaving like a string...

Link to comment
Share on other sites

EDITED:

 

OK I have narrowed the problem down. Basically inside my php file that deals with the request from the js file, I have a require_once(''); file. This is stopping it from working for some reason.

 

It still sends the correct output as mentioned above, but it must be changing it from a string into somthing else.. or it must be adding something to it that is hidden from view.

 

Here is the original php file:

 

 <?php
require_once('bookmark.php');

      header('Content-Type: text/plain');
      print 'name_ok';
?>

 

here is an edited version that works perfectly:

 

<?php
      header('Content-Type: text/plain');
      print 'name_ok';
?>

 

As you can see by removing this require function it works. So my question is how can I get it to work with the require function?

Link to comment
Share on other sites

Ok so I actually have 2 problems:

 

The first is that I cant include require_once('') in my php file that deals with the request. Anyone have ideas as to why that is?

 

The second problem is that I am actually over writting the output of the php file because when one form field has a value more than 1 in it, and then another has a value in it they will both cause a collision, because I am only sending one piece of data at a time, so therefore it will only deal with one request. I need to somehow make it deal with all requests but respond on an individual basis.

 

This piece of code needs changing somehow to do this:

 

  this.evalFormFieldState = function()
  {
    var self = reg;
    var form_data = '';
    var ajax = new Ajax();
    
    if(self.form.reg_name.value.length > 0)
    {
      form_data = 'name=' + escape(self.form.reg_name.value);
      ajax.doPost('check_reg.php', form_data, self.response);
    }
    if(self.form.reg_surname.value.length > 0)
    {
      form_data = 'surname=' + escape(self.form.reg_surname.value);
      ajax.doPost('check_reg.php', form_data, self.response);
    }
};

 

They are both overwritting each other when a user enteres something.

 

If no one responds, ill give up these posts in this thread, just trying to give as much detail to see if someone might spot something...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.