-1

Facing undefined variable issues while submitting form.Please help

Can anybody tell me as to what i am missing out?

I intend to call function 'process_input' for form-submission, and inside process input i am getting the ip/uid/pwd details.

                        $.ajax({
                                type : 'POST',
                                url : 'post_process.php',
                                data : {
                                        func : 'process_input'
                                },
                        }).done(function(data) {

                                // show the response
                                $("#response").html(data);

PHP

if (isset ( $_POST ['func'] ) && ! empty ( $_POST ['func'] )) {
        $func = trim ( $_POST ['func'] );
        switch ($func) {
                case 'process_input' :
                        process_input ();
                        break;
                        ..........
        }
}


function process_input() {
        header ( 'Content-Type: text/html; charset=utf-8' );

        if (isset ( $_POST ['ip'] ) && ! empty ( $_POST ['ip'] )) {
                $ip = trim ( $_POST ["ip"] );
        }
        if (isset ( $_POST ['uid'] ) && ! empty ( $_POST ['uid'] )) {
                $uid = trim ( $_POST ["uid"] );
        }
        if (isset ( $_POST ['pwd'] ) && ! empty ( $_POST ['pwd'] )) {
                $pwd = trim ( $_POST ["pwd"] );
        }

        echo "$ip $uid $pwd <br>";
...
}

Notice: Undefined variable: ip in on line 41

All the variable($ip $uid $pwd) comes as undefined

Jay Blanchard
  • 32,731
  • 15
  • 70
  • 112
  • Which variable(s) are you getting as undefined? – Mike Brant Oct 16 '14 at 13:55
  • the $ip, $uid, $pwd variables. – angshuman Oct 16 '14 at 13:56
  • Are you getting any errors? Add error reporting to the top of your file(s) right after your opening ` – Jay Blanchard Oct 16 '14 at 13:59
  • In addition to not initialising those variables in your PHP code, you're not even posting them to the server. Your ajax call is clearly only posting 1 variable, namely 'func'. You should post all required variables in your ajax function. Eg. data: {func : 'process_input', id:id, uid:uid, pwd:pwd} – Jonathan Spiller Oct 16 '14 at 14:04
  • So i did this and it worked.....var dataString = 'ip='+ ip + '&uid=' + uid + '&pwd=' + pwd + '&func=process_input'; But then, is that the right way to post a form ? – angshuman Oct 16 '14 at 14:23
  • @angshuman There are many different ways, each with pros and cons. You can find more here: http://stackoverflow.com/questions/1960240/jquery-ajax-submit-form – Jonathan Spiller Oct 16 '14 at 15:03

1 Answers1

0

The problem is on this line;

echo "$ip $uid $pwd <br>";

Define the variables with default values above the checks. As there can be situations where not all of them are set. Like so;

function process_input() {

    $ip = ''; 
    $uid = ''; 
    $pwd = '';

    header ( 'Content-Type: text/html; charset=utf-8' );

....
Matt Burrow
  • 8,547
  • 2
  • 28
  • 37