0

I have a small button which when pressed asks for a name and then adds the input to the URL which can then be processed by PHP. My code is below.

    function staff_prompt(url)
  {
  var url = url;
  var staffname=prompt("Please enter your Name below:","Staff Member Name");
  if (staffname == null)  {
  } 
  else if (staffname == "Staff Member Name") {
  alert('You must enter a value to continue');
  staff_prompt();
  }
  else if (staffname == "") {
  alert('You must enter a value to continue');
  staff_prompt();
  }
  else {
  var windowgo = url + "&staffmember=" + staffname;
  window.location = windowgo;
 }
 } 

My Button code is below

onclick="staff_prompt('index.php?app=orders&page=action&action=go_orderedviewtype=<?php echo $viewtype ?>&orderid=<?php echo $orderid ?>') 

Sometimes it works well, however other times it seems to go to "http://undefined....ect" I cannot replicate the situation, sometimes it happens and sometimes not

I am thinking that maybe the code is not very good... why would the javascript not detect sometimes the variable 'url'.

1
  • use qunit to test your function qunitjs.com Commented Dec 24, 2012 at 8:32

3 Answers 3

3

Yes, there is problem in your code.

You are calling staff_prompt() function from both of your else if conditions & you have not passed the url in it as a parameter, so it will definitely take it as undefined.

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

1 Comment

Thanks - I guess that's why it only happened sometimes :)
2

Here URL is undefined. You are calling the function with an undefined parameter:

staff_prompt();

So as long as you get into the else if-s you would be getting the undefined url.

Change your code to staff_prompt(url); and you would be fine.

Comments

0

When you call staff_prompt after an error, you do not pass it url.

function staff_prompt(url) {
    var url = url;
    var staffname=prompt("Please enter your Name below:","Staff Member Name");
    if (staffname == null)  {
    } else if (staffname == "Staff Member Name") {
        alert('You must enter a value to continue');
        staff_prompt(url);
    } else if (staffname == "") {
        alert('You must enter a value to continue');
        staff_prompt(url);
    } else {
        var windowgo = url + "&staffmember=" + staffname;
        window.location = windowgo;
    }
}

​ Example: http://jsfiddle.net/burlak/CsrTc/3/

Comments

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.