Jump to content

How to include require('') function


fry2010

Recommended Posts

Hi all. I did put this amongst another thread I made, but it wasnt really the main thing I asked. Also no one responded probably because I wasnt clear on anything.

 

But here is my problem. I have 3 files. One file is the ajax.js that does the HTTPRequest stuff, then my main check_reg.js file which deals with changing the state of things, and a php file, check_reg.php which takes the requests from the check_reg.js and gives it a response.

 

So in my php file if I use a require_once('bookmark.php') function it stops the ajax working. For some strange reason it still gives a response, yet the whole ajax just stops working.

 

So far I have been getting by without doing the require function, and just included all the functions I needed in that file. But I have a database connection function that I need to use, so I dont want to put that in there, plus I use other functions so it means doubling up on those functions.

 

I would just rather be able to include the require_once() function so if anyone has any ideas please let me know.

Link to comment
Share on other sites

No problem jackpf, I wasnt very clear but I figured out the main problem eventually.

 

Anyway this is really strange, because now when I include the require function it does actually work except for one small thing?? I must have changed somthing, but I cant remember what. I basically left it because it was so annoying.

 

Ill start from the top:

 

When a user enters input into form fields, the check_reg.js will recognise the user is typing, and send the values being typed to the php file.

 

The php file then analyses this data and if its ok it will send a response back to the response handler in the .js file. This will then change a div next to that form field to say 'OK'.

 

Now it is doing this for every field, except for the name field (before it wasnt doing any, when I removed the function 'require('')' it all worked though.).

 

So now I have the require('bookmarks.php') inside and everything works, except the name field. When I remove the require function, the name field works? Its dam strange.

 

Here is some of the .js fil:

 

  this.init = function()
  {
    var self = reg;
    self.ajax = new Ajax();
    self.form = document.getElementById('reg_form');
    self.promptDiv = document.getElementById('reg_status');
    self.name_div = document.getElementById('name_div');
    self.surname_div = document.getElementById('surname_div');
    self.username_div = document.getElementById('username_div');
    self.pass_div = document.getElementById('pass_div');
    self.pass2_div = document.getElementById('pass2_div');
    self.email_div = document.getElementById('email_div');
    self.button = document.getElementById('reg_submit');
    self.setPrompt('err', 'Not Complete');
    self.toggleEnabled(false, 'button');
    self.form.onsubmit = function()
    {
      return false;
    }
  };

  this.setPrompt = function(status, msg)
  {
    var self = reg;
    var reg_status_msg = document.getElementById('reg_status_msg');
    var promptDiv = self.promptDiv;

    reg_status_msg.className = status + '_reg_msg'; // 'ok', 'err'

    if(reg_status_msg.firstChild)
    {
      reg_status_msg.removeChild(reg_status_msg.firstChild);
    }

    reg_status_msg.appendChild(document.createTextNode(msg));
      
  };
    
  this.keyup = function(e)
  {
    var self = reg;

    if(!e)
    {
      e = window.event;
    }
    if(e.keyCode != 13)
    {
      self.evalFormFieldState();
    }    
    else 
    {
      if (self.enabled) 
      {
        self.submitData();
      }
    }

  };

  this.evalFormFieldState = function()
  {
    var self = reg;
    var form_data = '';
    var ajax = new Ajax();
    form_data = formData2QueryString(self.form);

    // Send the hidden field so that click and drag delete is recognised...
    if(self.form.reg_hidden.value.length > 0)
    {
      ajax.doPost('check_reg.php', form_data, self.response);
    }
    // ---------------------

    if(self.form.reg_name.value.length > 0)
    {
      ajax.doPost('check_reg.php', form_data, self.response);
    }
    if(self.form.reg_surname.value.length > 0)
    {
      ajax.doPost('check_reg.php', form_data, self.response);
    }
    if(self.form.reg_user.value.length > 0)
    {
      ajax.doPost('check_reg.php', form_data, self.response);
    }
    if(self.form.reg_pass.value.length > 0)
    {
      ajax.doPost('check_reg.php', form_data, self.response);
    }
    if(self.form.reg_pass2.value.length > 0)
    {
      ajax.doPost('check_reg.php', form_data, self.response);
    }
    if(self.form.reg_email.value.length > 0)
    {
      ajax.doPost('check_reg.php', form_data, self.response);
    }

    if((self.form.reg_name.value.length > 2) && (self.form.reg_surname.value.length > 2)
    && (self.form.reg_user.value.length > 5) && (self.form.reg_pass.value.length > 5)
    && (self.form.reg_pass2.value.length > 5) && (self.form.reg_email.value.length > 5))
    {
      ajax.doPost('check_reg.php', form_data, self.response);
    }

    if((self.form.reg_name.value.length == 0) && (self.form.reg_surname.value.length == 0)
    && (self.form.reg_user.value.length == 0) && (self.form.reg_pass.value.length == 0)
    && (self.form.reg_pass2.value.length == 0) && (self.form.reg_email.value.length == 0))
    {
      self.toggleEnabled(false, 'button');
    } 

    if(self.form.ext_code.value.length > 0)
    {
      exit;
    }
  };

  this.response = function(str)
  {
    var self = reg;
    var resp = str.split(',');
    var name = resp[0];
    var surname = resp[1];
    var user = resp[2];
    var pass = resp[3];
    var pass2 = resp[4];
    var email = resp[5];
    var all = resp[6];

    if(name == 'name_ok')
    {
      if(self.name_div.firstChild)
      {
        self.name_div.removeChild(self.name_div.firstChild);
      }
        self.name_div.appendChild(document.createTextNode('ok'));
    }
    else if(self.name_div.firstChild)
    {
        self.name_div.removeChild(self.name_div.firstChild);
    }

    if(surname == 'sur_ok')
    {
      if(self.surname_div.firstChild)
      {
        self.surname_div.removeChild(self.surname_div.firstChild);
      }
        self.surname_div.appendChild(document.createTextNode('ok'));
    }
    else if(self.surname_div.firstChild)
    {
        self.surname_div.removeChild(self.surname_div.firstChild);
    }

    if(user == 'user_ok')
    {
      self.username_div.className = 'reg_green';
      
      if(self.username_div.firstChild)
      {
        self.username_div.removeChild(self.username_div.firstChild);
      }
        self.username_div.appendChild(document.createTextNode('Available'));
    }
    else if(self.form.reg_user.value.length > 5)
    {
      self.username_div.className = 'reg_red';

      if(self.username_div.firstChild)
      {
        self.username_div.removeChild(self.username_div.firstChild);
      }
        self.username_div.appendChild(document.createTextNode('Username Taken'));
    }
    else if(self.username_div.firstChild)
    {
      self.username_div.removeChild(self.username_div.firstChild);
    }

    if(pass == 'pass_ok')
    {
      if(self.pass_div.firstChild)
      {
        self.pass_div.removeChild(self.pass_div.firstChild);
      }
        self.pass_div.appendChild(document.createTextNode('ok'));
    }
    else if(self.pass_div.firstChild)
    {
        self.pass_div.removeChild(self.pass_div.firstChild);
    }

    if(pass2 == 'pass2_ok')
    {
      if(self.pass2_div.firstChild)
      {
        self.pass2_div.removeChild(self.pass2_div.firstChild);
      }
        self.pass2_div.appendChild(document.createTextNode('ok'));
    }
    else if(self.pass2_div.firstChild)
    {
        self.pass2_div.removeChild(self.pass2_div.firstChild);
    }

    if(email == 'email_ok')
    {
      if(self.email_div.firstChild)
      {
        self.email_div.removeChild(self.email_div.firstChild);
      }
        self.email_div.appendChild(document.createTextNode('ok'));
    }
    else if(self.email_div.firstChild)
    {
        self.email_div.removeChild(self.email_div.firstChild);
    }

    if(all == 'all_ok' && self.form.reg_captcha.value.length > 2)
    {
      self.setPrompt('ok', 'OK to process');
      self.toggleEnabled(true, 'button');
    }
    else
    {
      self.setPrompt('err', 'Not Complete');
      self.toggleEnabled(false, 'button');
    }

  };

 

Ok here is the php file:

 

<?php
require_once('bookmark.php'); // I didnt have this here before and it all worked

  $name = $_POST['name'];
  $surname = $_POST['surname'];
  $user = $_POST['user'];
  $pass = $_POST['password'];
  $pass2 = $_POST['password2'];
  $email = $_POST['email'];

  $comma = ',';
  $count = 0;

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

  if(isset($surname))
  {
    if(valid_name($surname) && (strlen($name) < 40))
    {
      $surname = 'sur_ok';
      $count = $count + 1;
    }
  }

  if(isset($user))
  {
    if(valid_input($user) && (strlen($user) > 5 && strlen($user) < 21))
    {
      $conn = db_connect();
      $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
      
      $sql = 'SELECT COUNT(*) FROM useraccount WHERE username =:user';

      $stmt = $conn->prepare($sql);
      $stmt->bindParam(':user', $user);
      $stmt->execute();
      
      if($stmt->fetchColumn() == 0)
      {
        $conn = NULL;
        $user = 'user_ok';
        $count = $count + 1;
      }
    }
  }

  if(isset($pass))
  {
    if(valid_password($pass) && (strlen($pass) > 5 && strlen($pass) < 21))
    {
      $pass1 = 'pass_ok';
      $count = $count + 1;
    }
  }

  if(isset($pass2))
  {
    if($pass2 == '')
    {
    }
    else if($pass2 == $pass)
    {
      $pass2 = 'pass2_ok';
      $count = $count + 1;
    }
  }

  if(isset($email) && valid_email($email))
  {
    $email = 'email_ok';
    $count = $count + 1;
  }

  if($count == 6)
  {
    $all = 'all_ok';
  }

      header('Content-Type: text/plain');
      print $name;
      print $comma;
      print $surname;
      print $comma;
      print $user;
      print $comma;
      print $pass1;
      print $comma;
      print $pass2;
      print $comma;
      print $email;
      print $comma;
      print $all;

?>

 

Now if you notice in the php file, that I am also keeping a count with $count to check to see if that value is ok. If the count reaches +6, it will send the message $all = 'all_ok', and this will show at the top of the form saying 'OK to process..'. If it doesnt equall +6 it will show 'Invalid'.

 

The interesting thing about this is, although the 'ok' doesnt show next to the name field, the form DOES validate, meaning that the $count HAS reached +6. So if it has reached +6 it means that the if() statement in the php file has processed, so $name should and does return 'name_ok'. Yet it doesnt show in the field.....

 

Its crazy....

Sorry about the length, its hard to pinpoint the exact cause here...

Link to comment
Share on other sites

you know what, I think I will just post part of the code, rather than huge list like that, cause you aint gna have the time to sit through all that. I just realised how much I put in.

 

Anyway here is what I think is neccessary to look at:

 

the functions that deal with just the name and surname fields from the .js file:

 

 
<?php // i no its not php, just to colour it in...
/*
If the length of the name field is greater than 0, send the string 
typed to the php file ...
*/
   if(self.form.reg_name.value.length > 0)
    {
      ajax.doPost('check_reg.php', form_data, self.response);
    }
//  ....  and the surname field
    if(self.form.reg_surname.value.length > 0)
    {
      ajax.doPost('check_reg.php', form_data, self.response);
    }

/*
Here is the response handler that recieved either 'name_ok', or just ' '.
This will then either output 'OK' or it wont
*/
    if(name == 'name_ok')
    {
      if(self.name_div.firstChild)
      {
        self.name_div.removeChild(self.name_div.firstChild);
      }
        self.name_div.appendChild(document.createTextNode('ok'));
    }
    else if(self.name_div.firstChild)
    {
        self.name_div.removeChild(self.name_div.firstChild);
    }
// ... and the same for the surname
    if(surname == 'sur_ok')
    {
      if(self.surname_div.firstChild)
      {
        self.surname_div.removeChild(self.surname_div.firstChild);
      }
        self.surname_div.appendChild(document.createTextNode('ok'));
    }
    else if(self.surname_div.firstChild)
    {
        self.surname_div.removeChild(self.surname_div.firstChild);
    }

?>

 

Here is the php file:

 

<?php  $name = $_POST['name'];
  $surname = $_POST['surname'];

/*  $user = $_POST['user'];
  $pass = $_POST['password'];
  $pass2 = $_POST['password2'];
  $email = $_POST['email']; */

  $comma = ',';
  $count = 0;

  if(isset($name))
  {
    if(valid_name($name) && strlen($name) < 30)
    {
      $name = 'name_ok';
      $count = $count + 1;  // Note the count here is adding 1
    }
  }

  if(isset($surname))
  {
    if(valid_name($surname) && (strlen($name) < 40))
    {
      $surname = 'sur_ok';
      $count = $count + 1;
    }
  }

  if($count == 6)
  {
    $all = 'all_ok';   // Notice that if the count reached 6 here it will send a message that all fields are ok
  }

      header('Content-Type: text/plain');
      print $name;
      print $comma;
      print $surname;

/*      print $comma;
      print $user;
      print $comma;
      print $pass1;
      print $comma;
      print $pass2;
      print $comma;
      print $email;
      print $comma; */
    
      print $all;  /* This does actually send 'all_ok', even though the
                         name field message 'ok' doesnt send....*/
?>

 

Ok I think that is a bit better...

Link to comment
Share on other sites

I can't actually tell from that mass of code you posted :P

But why don't you try debugging it a bit?

 

Try echoing stuff back to the JS file from your PHP script, just to make sure it's making the connection.

 

And what exactly is happening? Is nothing being returned from the PHP script?

Link to comment
Share on other sites

Yeah I know its too much.

 

No the problem wasnt with the php file not sending data back. It did, and sent the correct message back. The if statement wasnt working.

 

But dont worry I just sorted it! Cant believe it, it always happens just as I need help, I figure it out.

 

Yeah I did what you say before to echo stuff out and check everything, and it was responding 'name_ok', yet the if statement to check the name wasnt excepting it.

 

The way I have got around it is a bit of a hack, but nothing major. Basically in the .js file I am dealing with the php response into arrays, and checking those arrays. I had the name_ok response to array[0], so I changed it to array[1] and moved all the others up 1 too. It Worked!? So that means that when you include a require() function it does somthing to it to stop sending the first bit of info.

 

Anyway cant beleive after all that its working just with a little hack. Thanks for the willingness to help tho guys.  ;D

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.