Undefined Variable Problem with PHP Function

Undefined variable problem with PHP function

This is because you're using the $pera variable (which exists only in the global scope) inside a function.

See the PHP manual page on variable scope for more information.

You could fix this by adding global $pera; within your function, although this isn't a particularly elegant approach, as global variables are shunned for reasons too detailed to go into here. As such, it would be better to accept $pera as an argument to your function as follows:

function provera($prom, $pera){
if (preg_match("/[0-9\,\.\?\>\.<\"\'\:\;\[\]\}\{\/\!\\\@\#\$\%\^\&\*\(\)\-\_\=\+\`[:space:]]/",$prom)){
echo "Nepravilan unos imena ili prezimina!";
echo $pera;
}
}

Undefined Variable Error While Calling a Function

Add global $con; to your function. It is a global variable and is not automatically accessible by your function.

Why my variable is undefined inside the function

Have a read about variable scope in the manual

$file and $newfile are global, therfore $newfile cannot be accessed locally in your function. Either move it into the function, pass it in as a parameter or as a last resort declare it as global in the function

How to fix undefined variable in PHP code?

The problem is your method requires a parameter to be given whenever called,
that parameter is $nohp in your Home controller method ubahdata.So if you want to solve this problem you either have to pass that parameter when you call that method or remove it.I don't understand the purpose of this variable and looking at your HTML code you passing that variable via POST method so you don't need to pass that as parameter, you can just remove it. This just my understanding.

public function ubahdata()
{
$user['nohp'] = $this->input->post('nohp');
$user['nama'] = $this->input->post('nama');
$user['alamat'] = $this->input->post('alamat');

$query = $this->mahasiswa_model->ubah_model_data($user, $user['nohp']);
}
public function ubah($nohp)
{
$data['mahasiswa'] = $this->mahasiswa_model->ubah_model($nohp);
$this->load->view('home/tampil', $data);
}

Undefined variable : function parameter (PHP)

I would do something like this (note you never want to echo out individual messages for email and password to stop hackers gaining information about which is correct:

session_start();
require_once('../model/pdo.inc.php');

//username and password will contain the posted resulte or FALSE
$username = usernameValidation();
$password = passwordValidation();
if (!$username OR !$password) {
echo 'Invalid username or password!';
die;
}
// PDO Query (SELECT ...)

// function for checking the username validation (not empty & Regex)
function usernameValidation() { // Username as parameter
if (!empty($_POST['username'])) {
$username = strtolower(htmlspecialchars($_POST['username'])); // username will be tranform to lowercase before regex matching

if (preg_match('#^[a-z0-9\.]{5,20}$#', $username)) { // 5 <= username lenght <= 20 in lowercase character to be valid
return $username; // return true when the username is valid
}
}
return FALSE;
}

// function for checking the password validation (not empty & Regex)
function passwordValidation() { // Password as parameter
if (!empty($_POST['password'])) {
$password = htmlspecialchars($_POST['password']); // Protect the password

if (preg_match('#^[a-zA-Z0-9\.-_@$()]{6,10}$#', $password)) { // 6 <= password length <= 10 character to be valid
return $password; // return true when password is valid
}
}
return FALSE;
}


Related Topics



Leave a reply



Submit