place id in javascript + detect $row statement in PDO

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
JanMolendijk
Posts: 208
Location: Holland Rotterdam

place id in javascript + detect $row statement in PDO

I have a little script to detect if a member is placed in database
but I dont understand how to detect $row in javascript

also I don`t known how to place a extra line in my PDO doc
for $row = $stmt->fetch(PDO::

index

Code: Select all

<script>
$(document).ready(function() {
$('#search-data').blur(function(e) {
    var value = $(this).val();
    liveUsernameSearch(value);
         
});
});

function liveUsernameSearch(val){
	$('#preloader-gif').show();	
	$.post('controller.php',{'search-data': val}, function(data){
			
		if(data == "Match Found")
		   $('#search-result-container').html("<span style='color:green;font-weight:bold;'> <?php ['name'] ?> <?php  $name ?>  Username is available</span>");
        else				
		   $('#search-result-container').html("<span style='color:red;font-weight:bold;'>Username is not available</span>");
	 $('#preloader-gif').hide();
	}).fail(function(xhr, ajaxOptions, thrownError) { //any errors?					
	       alert(thrownError); //alert with HTTP error									
	});
}


</script>
if(data == "Match Found")
$('#search-result-container').html("
Here I try to add the <?php echo $row['post_id'];?>

Code: Select all

	public function searchData($searchVal){		
		
		try {
			
			$dbConnection = $this->dbConnect();
			$stmt = $dbConnection->prepare("SELECT * FROM `user` WHERE `name` = :searchVal");			
			$stmt->bindParam(':searchVal', $searchVal , PDO::PARAM_STR);			
			$stmt->execute();
			
			$Count = $stmt->rowCount(); 
			//echo " Total Records Count : $Count .<br>" ;
             
			$result ="" ;
			if ($Count  > 0){				
				$result = "Match Found" ;
			}else{
				$result = "Match Not Found" ;
			}
			return $result ;

			
I would like to detect $row with this simple php code
but it is not working

Code: Select all

 $row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_LAST);

Admin
I think that in the controller.php file you must call the searchData() method with data from the js ajax.
Something like this:

Code: Select all

echo $objName->searchData($_POST['search-data']);
And that file must not return other thing.
But it is just an example, searchData() is part of a class, so, it depends of how that class is used.
In this example the $objName represents the object of that class.

Similar Topics