Problems with php code within a php code...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kyoru
Forum Commoner
Posts: 26
Joined: Mon Feb 13, 2006 9:35 pm

Problems with php code within a php code...

Post by kyoru »

Hey guys sorry for another question i keep running into issues...
Right now im building a form validator, a javascript one reads the forms first and if it works it loads a php code to check them.
I tested it and it works pretty good, however it is a seperate file named...shipping.php. Now it is a mix of html and php for example most of the start is html but the php code is at the end when the submit button is pressed. however i want to include this into my main layout code which is all php. This is the issue...

Code: Select all

$add_end = '<?php { 
		$_SESSION['session_page_error'] = ""
		include 'includes/cart_functions.php';
		$size_select = $_POST['size'];
		$quantity_select = $_POST['quantity'];
		
		switch ($size_select) {
		case 'small':
			$quantity_max = $small_stock;
			$small_up = $quantity_select;
			break;
		case ' medium';
			$quantity_max = $medium_stock;
			$medium_up = $quantity_select;
			break;
		case 'large';
			$quantity_max = $large_stock;
			$large = $quantity_select;
			break;
		case 'xlarge';
			$quantity_max = $xlarge_stock;
			$xlarge = $quantity_select;
			break;
		}
		if ($quantity_max < $quantity_select) {
			$_SESSION['session_page_error'] = "Quantity selected is larger then stock.";
			}
		else if ($quantity_select == '') {
			$_SESSION['session_page_error'] = "Please enter a quantity.";
		}
		else if ($quantity_select == '0') {
			$_SESSION['session_page_error'] = "Please enter a quantity greater then 1.";
		}
		else {
			AddToCart($sid,$directory,$small_up,$medium_up,$large_up,$xlarge_up);
		}
}
?>';
as you can see the single quotes won't work in the php file but if i add slashes, the php code won't work....what do i do to fix this?
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

Im guessing here...

Create a new page and put your code in it.
Then call the page in your main layout

Code: Select all

include_once('includes/blah.php');
As far as I know you can't put PHP inside PHP.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

put the code into a function.
kyoru
Forum Commoner
Posts: 26
Joined: Mon Feb 13, 2006 9:35 pm

Post by kyoru »

this is hard to describe...I cannot include it because it is and include file already so i need to somehow make it a variable...

main page < includes sub menu < includes the form validation

i'll try to function, that way i can call it but still include the code avoiding the quotes i think thank you!
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

kyoru wrote:this is hard to describe...I cannot include it because it is and include file already so i need to somehow make it a variable...

main page < includes sub menu < includes the form validation

i'll try to function, that way i can call it but still include the code avoiding the quotes i think thank you!
You can include a page, and then include another page inside the included page.
kyoru
Forum Commoner
Posts: 26
Joined: Mon Feb 13, 2006 9:35 pm

Post by kyoru »

anthony88guy wrote:
kyoru wrote:this is hard to describe...I cannot include it because it is and include file already so i need to somehow make it a variable...

main page < includes sub menu < includes the form validation

i'll try to function, that way i can call it but still include the code avoiding the quotes i think thank you!
You can include a page, and then include another page inside the included page.
thats not the real issue but the content are in variables...and only the first page has the echo statement, but the include of the include has php code in it with single quotes so the backslashes will be echoed making the code useless
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Try this and see what it does...

Code: Select all

<?php
eval($add_end);
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

eek, don't recommend using eval(). 99.95% of the time, the same thing can be accomplished in better ways without adding the possible security risks.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I am not a big fan of eval either, but it appears that the sugestions that have been offered thus far have not been to this developers satisfaction. My thought is that he is probably using this code as part of an IF check, which would not necessarily require the code to be thrown into a variable but can be directly executed. If he can't include it in an include and he can't run it out of a function, then the next easiest, albeit more insecure, way to achieve what he wants is by eval.

I do agree that he should find a different way to execute this code.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I don't get this. Please explain what you are trying to acheive because every way I've looked at it just doesn't make any sense. What should be in the variable? the string or the result? I ask because there's nothing returned from what I can see, so even a function would be pointless if it's supposed to assign to a variable :?

If you explain a little further what the goal is we should be able to offer more solid solutions ;)

Note: Your syntax for the switch construct is wrong. It should be:

Code: Select all

case 'foo':
$do = something();
break;
Notice the use of a full colon after the "case" statement as opposed to a semi-colon? :)
kyoru
Forum Commoner
Posts: 26
Joined: Mon Feb 13, 2006 9:35 pm

Post by kyoru »

:oops: i figured out the problem my self, because the variable has single quotes in it i entered the backslashes to escape them, i figured that the baclk slashes would be printed out when i echo them rendering my php code useless, but i overthinked it and it turns out i was wrong :|
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

kyoru wrote::oops: i figured out the problem my self, because the variable has single quotes in it i entered the backslashes to escape them, i figured that the baclk slashes would be printed out when i echo them rendering my php code useless, but i overthinked it and it turns out i was wrong :|
:) Glad you got it sussed.
Post Reply