Jump to content

Variable Returning Empty


l33tmeerkatslol

Recommended Posts

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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']);

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.