Closed Thread Icon

Topic awaiting preservation: More obvious answers you hate to give (PHP) (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=22489" title="Pages that link to Topic awaiting preservation: More obvious answers you hate to give (PHP) (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: More obvious answers you hate to give (PHP) <span class="small">(Page 1 of 1)</span>\

 
Alexer
Nervous Wreck (II) Inmate

From: Juneau, Alaska
Insane since: Jun 2004

posted posted 07-09-2004 02:01

Just for the record, I'm not one to ask other people to debug my code for me, but I've been looking at this for over an hour, and no answer has come. I'm trying to create a form that takes a URL and creates a clickable link (even adding an http:// and an <A HREF.. all that good stuff). However, I always get this error, followed by a very half-assed version of my intended results..

quote:
Warning: Invalid range end in /home/apache/test/www/richard/HandleForm.php on line 14


quote:
Your submisson----has been received!



The URL (depending on what is entered in the form) should appear in between the second hyphen and the third hyphen. Here's the code of the document that handles the data from the form:

quote:
<HTML>
<HEAD>
<TITLE>Using Regular Expressions</TITLE>
<BODY>
<?php
/* This page receives and handles the data generated by "form.html". */
if (($Array["FirstName"]) AND ($Array["LastName"])) {
$Array["Name"] = $Array["FirstName"] . " " . $Array["LastName"];
} else {
print ("Please enter your first and last names.<BR>\n");
}
$Pattern = "(http://)?([^[:space:]]+)([[:alnum:]\.,-_?/&=])";
$Replace = "<a href=\"http://\\2\\3\" target=\"_new\">\\2\\3</a>";
$Array["URL"] = eregi_replace($Pattern, $Replace, $Array["URL"]);
print ("Your submisson--$Array[URL]--has been received!<BR>\n");
?>
</BODY>
</HTML>



Line 14 (which the error puts hatred towards) seems fine to me. Help appreciated. Thanks in advance

code-headed intern

H][RO
Bipolar (III) Inmate

From: Australia
Insane since: Oct 2002

posted posted 07-09-2004 02:28

Generally when you get out of range things its because you are trying to get to invalid bounds of an array. (or something doesnt exist etc)

To me it looks like you are missing your quotation marks on the last line

code:
print ("Your submisson--$Array[URL]--has been received!<BR>\n");



Shouldnt this be

code:
print ("Your submisson--" . $Array["URL"] . "--has been received!<BR>\n");



Try that see what happens.

norm
Paranoid (IV) Inmate

From: [s]underwater[/s] under-snow in Juneau
Insane since: Sep 2002

posted posted 07-09-2004 07:19

I don't see any place where you are populating the $Array[] array. My bet is that you have no values for $Array["FirstName"] or
$Array["LastName"]. Try a few echo or print statements and find out. tracking the value of your variables is the first and probably most important step in debugging code.

I think what you want is $_POST['FirstName'] and so forth....
When data is sent from an HTML form, it is automaticly put into the $_POST[] or $_GET[] array. You can also find them in $HTTP_POST_VARS[] or $HTTP_GET_VARS[] but these have been depreicated in favor of the globals _POST and _GET.

What's up with the capital 'A' for $Array? This is usually used for class names, while all capitals is used for Pre-defined SuperGlobals and also for Constants. Not quite a flogging offense , but very close!

One more tidbit-

To execute a block of code if the data from a form has been received try the following

code:
if (isset($_POST['submit'])){
//every form has a submit button, this works if it's name is 'submit'

function doStuff(){
echo "here is some Stuff";
}

doStuff();

}




happy coding to ya

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 07-09-2004 09:13

would any of you guys count?

Line 14 is where he's using his regexps for the first time...
and he's setting it in line 12:
$Pattern = "(http://)?([^[:space:]]+)([[:alnum:]\.,-_?/&=])";
and I don't know much eregs, but I'd bet that should read:
$Pattern = "(http://)?([^:space:]+)([:alnum:\.,-_?/&=])";

So long,
Tyberius Prime

PS: perl style regular expressions (preg) are much more common... I'd advise to learn them instead.

H][RO
Bipolar (III) Inmate

From: Australia
Insane since: Oct 2002

posted posted 07-09-2004 16:16

Yeh i missed a line, but he is still missing quotation marks when he is calling that part of the array, i wouldnt be suprised if it gave an error. Or does this work when the entire line is in quotation marks? Not sure why you need an array there anyhow, bit strange if you are only taking in one set of variables? Do you really get an advantage from it. But thats irrelevant here...

I dont do many reg expressions either, and yes you need to make sure you are getting the variables from the form as norm said.


Do some general debugging, hard code some variables etc and it shouldnt be too hard to find out exactly what is causing the error. Just because it says error on line 14, doesnt always mean it is dont forget, often it falls in the line before or after.

Nehow.. goodluck

Alexer
Nervous Wreck (II) Inmate

From: Juneau, Alaska
Insane since: Jun 2004

posted posted 07-09-2004 18:16
code:
print ("Your submisson--" . $Array["URL"] . "--has been received!<BR>\n");



I could see how this works, having dipped into Java a bit. However, in PHP it gives me a parse error.

quote:
Yeh i missed a line, but he is still missing quotation marks when he is calling that part of the array, i wouldnt be suprised if it gave an error. Or does this work when the entire line is in quotation marks?



Are you reffering to $Array ["URL"]? Indeed, I did not include the quotations. According to Larry Ullman's PHP for the World Wide Web, the quotations are not an absolute requisite, and should be avoided when in the middle of a set of quotes to avoid syntax errors. However, this same book is the one I retrieved the code from so...

I used norm's doStuff function. I am indeed getting the variables. I checked the code on the form, and the the variables are parallel.

code:
URL <INPUT TYPE=TEXT NAME="Array[URL]" SIZE=60><BR>



quote:
Not sure why you need an array there anyhow, bit strange if you are only taking in one set of variables? Do you really get an advantage from it. But thats irrelevant here...



Just for the record, this is an excercise and not a practical application. Therefore, it may have needless code for the sake of pounding the lesson into my head.

Thank you all for your help. I will continue to bang my head on the keyboard until I solve this. You definetly have my brain warmed up for the day.

code-headed intern

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 07-09-2004 18:47

You should always put quotes around your associative names. I don't care what that book says. Unquoted hash keys indicate that the key is a constant and is a bad code practice. Use single quotes instead of doubles if you're worried about it.

code:
print ("Your submisson--" . $Array['URL'] . "--has been received!<BR>\n");



Next turn on error reporting always do this during development, it not only tells you errors but gives warnings on some bad practices.

error_reporting(E_ALL)

After that try simplifying your regular expressions and replace patterns to try to reduce the potential problems.



.:[ Never resist a perfect moment ]:.

Alexer
Nervous Wreck (II) Inmate

From: Juneau, Alaska
Insane since: Jun 2004

posted posted 07-12-2004 18:32
code:
print ("Your submisson--" . $Array['URL'] . "--has been received!<BR>\n");



I tried this, (note that URL now has quotations), but it returned the following error, which I think is the servers way of telling me it hates me:

quote:
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /home/apache/test/www/richard/HandleForm.php on line 15



EDIT: Amazing what a little syntax fixing can do. The error stated above has dissapeared, but I still have my original problem. Ah me.

code-headed intern

(Edited by Alexer on 07-12-2004 18:57)

« BackwardsOnwards »

Show Forum Drop Down Menu