Lale Lale -4 years ago 80
PHP Question

PHP condition check and AJAX calling issues

got some kind help here recently so I thought I'd ask couple more questions, 2 to be exact.

Problem 1.

This is related to one of my previous questions can be found here: PHP condition check fail

Here's the code:

<?php

$username = (isset($_GET['username']) ? $_GET['username'] : null);

$fileName = "users.txt";

$users = file_get_contents( $fileName );
$objUsers = json_decode( $users );

// iterate through the list and find John
for( $i = 0; $i < count($objUsers); $i++ ){

if($objUsers[$i]->username == $username){
echo '<div>John is found</div>';
} else {
echo '<div>John is NOT found</div>';
break;
}
}
?>


The trouble is now it always and everytime returns "John is found" even if I typed "Charles" into the username field as it iterates through the whole list and will eventually find "John", no matter what. The way I need to change the code is to return "John is found" only when "John" is typed into username field.
I think it is a relatively easy problem for those of you experts out there but my OOP experience is not very helpful with this syntax.

Problem 2.

I would like to trigger an ajax call after a click event has done some stuff.

Code:

$("#div1").click(function(){

// if condition is true
if (...){ // working
// then if this condition is also true
if (...){ // working
var link = "do-some-stuff.php" // working
$.notify("task is done"); // working
$.getJSON( link, function( input ){});
// how to refer to div2 or should i just paste the ajax function here?
// tried this latter one, no success
}

$("#div2").click(function(){
$.ajax({
url: "other-suff.php",
type: "GET",
success: function(data){
$('#div3').html(data);
}
});
}


Thanks for all the suggestions!

Answer Source

Regarding your problem 1:

<?php

$username = (isset($_GET['username']) ? $_GET['username'] : null);

$fileName = "users.txt";

$users = file_get_contents( $fileName );
$objUsers = json_decode( $users );

// iterate through the list and find John
for( $i = 0; $i < count($objUsers); $i++ ){

    if($username=='John'){
        if($objUsers[$i]->username == $username){
            echo '<div>John is found</div>';
        } else {
            echo '<div>John is NOT found</div>';
            break;
        }
    }else{
        echo '<div>You are not looking for John</div>';
    }
}
?>

added an if to see if you tiped John -> look for John in the txt. Else alert that you didn't type John in the box

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download