Lester1992
Lester1992

Reputation: 493

How to add validation(javascript) in php?

let say that, i want to my change password in php code then it will validate by the use of javascript. before it return to my index page or it will popup on index page. how can i do it? any trick that you can suggest? :)

<?php
  include('config2.php');
  error_reporting(E_ERROR | E_PARSE);
    if (mysqli_connect_errno()){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $val = $_GET['val1'];
    session_start();
    $ak = $_SESSION['autokey'];

    $sql = "UPDATE tbl_user SET password = '". md5($val) ."' WHERE autokey = '$ak'";
    mysql_query($sql);

    header("location:index");
?>

thanks in advance :)

Upvotes: 1

Views: 56

Answers (1)

You could change your code block like this..

$sql = "UPDATE tbl_user SET password = '". md5($val) ."' WHERE autokey = '$ak'";
mysql_query($sql);
if(mysql_affected_rows())
{
    echo "<script>alert('Password was successfully changed !');</script>";
    echo "<script>window.location='index.php'</script>";
} else
{
    echo "<script>alert('Password was not changed');</script>";
    echo "<script>window.location='index.php'</script>";
}

As the comment says.. You are mixing up mysql_* and mysqli_*. Change that first.

Sidenote: Switching to PreparedStatements is even more better to ward off SQL Injection attacks !

Upvotes: 3

Related Questions