1

I have a form and the url for submitting the form will generated dynamicly

var Var1 = new Array();
var Var2 = new Array();
if(Var1.length === 0)
$(this).attr('action', 'http://localhost/Method' ).submit();
if(Var1.length != 0 && Var2.length === 0)
$(this).attr('action', 'http://localhost/Method/Var1').submit();
if(Var1.length != 0 && Var2.length != 0)
$(this).attr('action', 'http://localhost/Method/Var1/Var2').submit();

and all that URLs fires one method in the server and it is

public function Method(){}
public function Method(Var1){}
public function Method(Var1 , Var2){}

is there anyway to make all the last 3 methods as one method? something like this:

public function Method(Var1, Var2){
    if(  condition for Var1 ){// doSomthing}
    if(  condition for Var2 ){// doSomthing}
}
3
  • Please indent your code, we shouldn't do it for you. Commented May 20, 2012 at 17:57
  • @gdoron I am talking about the PHP end. is he using any PHP framework? Commented May 20, 2012 at 17:59
  • i am using php , and i wrote it , but someone edit it Commented May 20, 2012 at 18:01

3 Answers 3

2

If you need this function for PHP, you can use func_get_arg and func_num_args:

public function Method() {
    $numArguments = func_num_args();

    if ($numArguments >= 1) {
        $argument1 = func_get_arg(0);
        // Do something with argument 1
    }

    if ($numArguments >= 2) {
        $argument2 = func_get_arg(1);
        // Do something with argument 2
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

ok thank you but your code will make an error if there is just 1 argumet , code argument2 will be undifiend
@LinCR Actually func_get_arg returns false if the argument doesn't exist, hence the two tests checking if the arguments exist or not.
i got this error Notice: Undefined offset: 0 but my method is Method(var1, var2)
@LinCR I've changed the function to use func_num_args to avoid the warning.
2

If all the three url calls a single method in your server you can use the default argument mechanism.

public function Method($Var1=null, $Var2=null){
    if(is_null($Var1)){// doSomthing}
    if(is_null($Var2)){// doSomthing}
}

Obviously to map these urls you need to use some sorts of router logic. And the router must dispatch the proper method of the object.

For exmaple if your url is something like /index.php/Object/method/param1/param2, index.php should create the proper object first.

$parts = explode('/', $_SERVER['REQEUST_URI']);
array_shift($parts); // emtpy part
array_shift($parts); // index.php
$cls = array_shift($parts) // Object
$obj = new $cls;

And then dispatch the method.

$method = array_shift($parts);
call_user_func_array(array($obj, $method), $parts);

2 Comments

OMG , i didn't understant what you worte , because i am new to PHP and new to jQuery , i upvote your answer , really thank you for help , and sorry cos i didn't understant ur work , my english is even bad to , really thank you
Its important that you understand my code. because almost same code is running on your web app.
0
public function Method(){
      $root = 'http://localhost/Method/',
      $Var1 = func_get_args(0) || '',
      $Var2 = func_get_args(1) || '';

  if($Var1 && $Var2) $root .= $Var1  + '/' + $Var2;
  else if($Var1 && !$Var2) $root .= '/' + $Var1;
}

3 Comments

thank you dear, you are always helping me , but this time i was asking about the function on the server , not on my jQuery
That PHP isn't even valid... you don't define variables with var, nor do you define variables without $. The || trick also doesn't work in PHP, and you can't + strings together as you have above.
sorry , other answer helps me more , really sorry , i am sosryr, and realy very very thank you man

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.