Page 1 of 1

Problems with php code within a php code...

Posted: Sun Feb 19, 2006 7:16 pm
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?

Posted: Sun Feb 19, 2006 7:27 pm
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.

Posted: Sun Feb 19, 2006 7:37 pm
by feyd
put the code into a function.

Posted: Sun Feb 19, 2006 7:47 pm
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!

Posted: Sun Feb 19, 2006 8:09 pm
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.

Posted: Sun Feb 19, 2006 9:15 pm
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

Posted: Sun Feb 19, 2006 9:23 pm
by RobertGonzalez
Try this and see what it does...

Code: Select all

<?php
eval($add_end);
?>

Posted: Sun Feb 19, 2006 9:30 pm
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.

Posted: Sun Feb 19, 2006 9:53 pm
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.

Posted: Mon Feb 20, 2006 2:32 am
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? :)

Posted: Mon Feb 20, 2006 3:56 am
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 :|

Posted: Mon Feb 20, 2006 5:19 am
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.