Jump to content

Passing string through a function using return


tec-4

Recommended Posts

Hello everyone,

 

Pretty much what I am trying to do is run a query and based of the queries result, return a string - as of now it is turning up blank and cant seem to pass any strings through the function.

 

Current function:

 

function addNewBookmark($hash,$url,$title,$username){

   

     

$query = "INSERT INTO bookmarks (hash,url,title,username) VALUES '".md5($hash)."', '".$url."', '".$title."', '".$username."')";
      $result = mysql_query(query, $this->connection);
      $message = '';
      if(mysql_affected_rows($this->connection)!=1) {

        	$message = 'This URL already exists in the database!';
                return $message;
        }
        else
        $message = 'The URL was shared!';
        return $message;
        
           }

 

The above code seems to run fine, just cant seem to pass a string...read that you can possibly use a __toString function?  but that keeps throwing errors when I try to work with it.

 

Thanks all!

Oh, okay - my bad for the typo...thanks for the catch, though.

 

Maybe it is returning but perhaps I just may not be handling it correctly?

 

Essentially what I am trying to do is process that query and return the correct message to a script that will in turn take the $message and hand it off to a JavaScript function and echo it like so:

 

<?php
//connect to database class, process query and return message
$db->addNewBookmark($hash,$url,$title,$current_user);

?>

/* JavaScript Code */

function displayMessage(str)
{
// Using pure JavaScript to create and style a div element
    
var d = document.createElement('div');

with(d.style)
{
    	// Applying styles:
        
	position='fixed';
	width = '350px';
	height = '20px';
	top = '50%';
	left = '50%';
	margin = '-30px 0 0 -195px';
	backgroundColor = '#f7f7f7';
	border = '1px solid #ccc';
	color = '#777';
	padding = '20px';
	fontSize = '18px';
	fontFamily = '"Myriad Pro",Arial,Helvetica,sans-serif';
	textAlign = 'center';
	zIndex = 100000;
        
	textShadow = '1px 1px 0 white';

	MozBorderRadius = "12px";
	webkitBorderRadius = "12px";
	borderRadius = "12px";

	MozBoxShadow = '0 0 6px #ccc';
	webkitBoxShadow = '0 0 6px #ccc';
	boxShadow = '0 0 6px #ccc';
}

d.setAttribute('onclick','document.body.removeChild(this)');
    
    // Adding the message passed to the function as text:
d.appendChild(document.createTextNode(str));

    // Appending the div to document
document.body.appendChild(d);

    // The message will auto-hide in 3 seconds:
    
setTimeout(function(){
	try{
		document.body.removeChild(d);
	}	catch(error){}
},3000);
}

<?php 

// Adding a line that will call the JavaScript function:
echo 'displayMessage("'.$message.'");';
}
?>

 

The query runs and seems to process correctly but the $message in the last couple lines  (echo 'displayMessage("'.$message.'");';) does not seem to echo the message but the JS pop-up will still occur....however, if I just put the straight query in this page itself, like so:

 

mysql_query("	INSERT INTO bookmarks (hash,url,title,username)
			VALUES (
				'".md5($_GET['url'])."',
				'".$_GET['url']."',
				'".$_GET['title']."',
          '".$current_user."'
			)");

$message = '';
if(mysql_affected_rows($link)!=1)
{
$message = 'This URL already exists in the database!';
}
else
$message = 'The URL was shared!';

 

it will show the appropriate message in the JS popup.  Do I need to "hand it over" in a different way or something?

 

Thanks for the help and hope the above makes sense.

you are returning $message..but this variable is not available outside of the functions scope.. I think this may be where you are confused.. to get the returned value.. you muse set the function to a variable..

 

$message = $db->addNewBookmark($hash,$url,$title,$current_user); //arguments filled out appropriately.

 

now $message (or whatever you name the variable) will contain the value of your return statement..

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.