Jump to content

this seems to be a common title: My PHP function is not working


Scutterman

Recommended Posts

Ok, I have just started to learn PHP and AJAX. I have been meaning to learn for a while, but I kind of forced myself to learn by creating a chatroom out of them. It was easier than i thought, since I found a pre-made code, and since php is so similar to javascript I found it easy enough to mod.

 

Everything was fine, until I decided to try and add in bb code. I wrote the following function:

 

function foo_bb($searchFor, $out1){

// $foo_now will store the string that will replace the bb code
// set it to bold for the first one
$foo_now = "font-weight: bold;";

// replace the bb code with html code for bold
while (strpos($out1, "[".$searchFor."]") != FALSE && strpos($out1, "[/".$serarchFor."]") != FALSE) {
$out1 = str_ireplace("[".$searchFor."]","<span style=".$foo_now.">", $out1);
$out1 = str_ireplace("[/".$searchFor."]","</span">", $out1);
}

// set $foo_now to underline
$foo_now = "text-decoration: underline;";

// search for underline bb code
$searchFor = "u";

// replace the bb code with html code for underline
while (strpos($out1, "[".$searchFor."]") != FALSE && strpos($out1, "[/".$serarchFor."]") != FALSE) {
$out1 = str_ireplace("[".$searchFor."]","<span style=".$foo_now.">", $out1);
$out1 = str_ireplace("[/".$searchFor."]","</span">", $out1);
}

// set $foo_now to italic
$foo_now = "font-style: italic;";

// search for italic bb code
$searchFor = "i";

// replace the bb code with html code for italic
while (strpos($out1, "[".$searchFor."]") != FALSE && strpos($out1, "[/".$serarchFor."]") != FALSE) {
$out1 = str_ireplace("[".$searchFor."]","<span style=".$foo_now.">", $out1);
$out1 = str_ireplace("[/".$searchFor."]","</span">", $out1);
}

// return the modified message
return $out1;

}

 

and I call it using:

$out = foo_bb("b",$out);

 

The php file has been called using AJAX xml http requests:

   xhr.open("GET", "data2.php?m=" + message + "&c=" + txtColor + "&t=" + time1 + "&g=" + time2,  true);
   xhr.send(null); 

 

all variables passed to the php function are correct, and xhr is what has been used to set up the request

 

ok. A few facts.

* I have gone through at least half a dozen tutorials, and I cannot see what's wrong with the function.

* If I comment out the parts that call the function, I get the same error.  I need to comment out the function itself to get the chatroom working again.

* The only error is from the xhr.status, which I have displaying whenever the status is not 200. The error is 500 [server] which, in combination with everything else, lead me to believe that the function is the problem.

 

The source files are:

php file: http://scutterman.com/help/data.txt

html file: http://scutterman.com/help/index.htm.txt

 

ok, I think that's about it...

I hope that is enough detail, without being overly complex...

 

TIA

~~Scutterman~~

Link to comment
Share on other sites

your function won't compile as written. if you're not seeing that error, you should ensure error_reporting is turned on.

 

i suggest that you use a text editor that will highlight 'off' areas in a different color so you can see, for instance, where you have a double-quote and shouldn't have one.

Link to comment
Share on other sites

It can be really hard to debug server side scripts invoked via XHR.  The best thing to do is create a test script that calls the code invoked via XHR to make sure it is working correctly.  Then use it for your actual XHR requests.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.