l33tmeerkatslol Posted March 14, 2011 Share Posted March 14, 2011 I'll try to explain this as easily as possible. I'm working on making a little link shortener site just for fun/practice as a beginner with php. I originally used the source code 'lilurl' as a base to start out, but now I'm adding stuff to it for actual practice. I need some help though. I have two files, my index and my external php file. The external file is called on at the beginning of the index with require_once. The user submits a url into the PHP_SELF form and a url is returned, and it works all great. Now I'm trying to add the option to make a custom url suffix. <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> <fieldset> <label for="longurl">Enter a URL:</label> <input type="text" name="longurl" id="longurl" /> <input type="text" name="suffix" id="suffix" /> <input type="submit" name="submit" id="submit" value="Shrink!" /> </fieldset> </form> [/code] The form looks like this. I added the 'suffix' textbox. So after the user submits the code, it goes back up to the top (phpself). A variable is set for the url the user has inputted as "$longurl", like this. $longurl = trim(mysql_escape_string($_POST['longurl'])); So I added this. $suffix = ($_POST['suffix']); And to check if it worked, I went down the the bottom of the index and had it echo the suffix variable. When I entered in code into the suffix text box, it would be echoed at the bottom of the page, so the variable did receive the value. So here's where the external file comes in. It is called on by require_once at the very beginning of the index. It is full of functions. One of the functions generates a random string of numbers (that I coded) to be used as the url suffix (uses the $id variable). So my plan was to erase the $id = $rand1 . blah blah stuff and put $id = $suffix. $suffix being the variable from the index that the user inputted. Make sense? Well whenever I try it, it always acts the like the variable is empty, even when I echo it on the index it returns the value of the textbox. Does anyone know what I'm doing wrong? Could it be because since I call on $suffix in the external file, and it's only been declared in the index that it's treating it like a new/empty variable? Someone also explained it to me like this. -I call upon the external file. -I declare the variable is the value of the text box. (Which means it's currently empty because the form has not been submitted.) -Then, the form is submitted as PHP_SELF. -The external file is called upon again. -The variable is then assigned a value. It's hard to explain, but basically that the variable in the external file is not told about the new value of the variable because they are declared after the external file is called upon? :\ So I tried moving the $suffix = ($_POST['suffix']); before the external file is called upon, but the same thing happens. It's hard to explain the situation, but I've tried a lot of things and can't get it to work. Quote Link to comment https://forums.phpfreaks.com/topic/230622-variable-returning-empty/ Share on other sites More sharing options...
Brian W Posted March 14, 2011 Share Posted March 14, 2011 Are you working with error_reporting( E_ALL) ? You should be, it helps track down issues. Posting the lines around where the problem is or the entire code (with sensitive data **** out) will help you get helped. Side note: be sure you to use mysql_real_escape_string on $_POST['suffix'] as well. Quote Link to comment https://forums.phpfreaks.com/topic/230622-variable-returning-empty/#findComment-1187453 Share on other sites More sharing options...
frost Posted March 14, 2011 Share Posted March 14, 2011 something to try at the top of your file: print_r($_POST); This will spit out everything that is posted so you can see it. Also try: $suffix = $_POST['suffix']; //removed brackets from above echo $suffix; Quote Link to comment https://forums.phpfreaks.com/topic/230622-variable-returning-empty/#findComment-1187464 Share on other sites More sharing options...
Brian W Posted March 14, 2011 Share Posted March 14, 2011 Hmm, the brackets may be having it return a boolean... so rather than removing the brackets use them for the mysql_real_escape_string function that you really should use. $suffix = mysql_real_escape_string($_POST['suffix']); Quote Link to comment https://forums.phpfreaks.com/topic/230622-variable-returning-empty/#findComment-1187489 Share on other sites More sharing options...
l33tmeerkatslol Posted March 16, 2011 Author Share Posted March 16, 2011 Could it be that the variable inside the function is local, therefore not receiving the value from the global variable declared in the index? :-\ Quote Link to comment https://forums.phpfreaks.com/topic/230622-variable-returning-empty/#findComment-1188293 Share on other sites More sharing options...
l33tmeerkatslol Posted March 17, 2011 Author Share Posted March 17, 2011 I did manage to get it working. I moved the $suffix= statement into the external file and it all seems to be working. Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/230622-variable-returning-empty/#findComment-1188520 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.