Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need assistance with the syntax of PHP when submitting a form

Status
Not open for further replies.

rleyba828

Technical User
Sep 29, 2004
53
US
Hi Team,

May I just request some assistance with some form syntax in PHP.


I am getting stuck with the coding of my form below as I want it to output this URL:



Code:
[URL unfurl="true"]http://192.168.1.102/Integra/index.php?-table=FIREWALL_INTERFACES&Lower_Dec=<=167838722&Upper_Dec=>=167838722[/URL]



...where 167838722 is the value of the variable --> "target_ip" that was submitted by the user.

From the form below, I just get this:


Code:
[URL unfurl="true"]http://192.168.1.102/Integra/index.php?target_ip=123456&-action=list&-table=FIREWALL_INTERFACES&Lower_Dec=%3C%3D[/URL]


I am not sure how to evaluate the variable "target_ip" during submit time and substitute this as the parameter for the rest of the URL.



The snippet of the form I have so far is this:
==============================================================
class tables_FIREWALL_INTERFACES {

function block__before_body() {
echo "This is some text test<p>";

}

function block__actions_menu_head() {
echo "<form action='index.php' method='get'>";
echo " Search IP address: <input type='text' name='target_ip' />";
echo " <input type='hidden' name='-action' value='list'/>";
echo "<input type='hidden' name='-table' value='FIREWALL_INTERFACES'/>";
echo "<input type='hidden' name='Lower_Dec' value='<='$target_ip/>";
# echo "<input type='hidden' name='Upper_Dec' value='>='$target_ip/>;
echo "<input type='submit'/>";
echo "</form>";


}


}

========================================================

Thanks very much for any suggestions.
 
The characters are 'special' in urls and so are encoded for use.
Decode the URL in php using urldecode
Code:
$lower = urldecode($_REQUEST['Lower_Dec']);
 
Hi Jpadie,

Thanks for that.

I have modified my code to be:

<?
class tables_FIREWALL_INTERFACES {

function block__before_body() {
echo "This is some text test<p>";

}

function block__after_search_form_submit() {
$lower = urldecode($_REQUEST['Lower_Dec=<=']);
echo "<form action='index.php' method='get'>";
echo " Search: <input type='text' name='target_ip' />";
echo " <input type='hidden' name='-action' value='list'/>";
echo "<input type='hidden' name='-table' value='FIREWALL_INTERFACES'/>";
echo "<input type='hidden' name=$lower value=123456>";
echo "<input type='submit'/>";
echo "</form>";


}


}

But the output couldn't show the decoded "Lower-Dec=<=". thanks for all the help.


Code:
[URL unfurl="true"]http://192.168.1.102/Integra/index.php?target_ip=44444&-action=list&-table=FIREWALL_INTERFACES&value%3D123456=[/URL]
 
i think that your business logic may be incorrect. the only piece of information you are asking from the user is the target ip address. so the form should just contain that information. all the other information you should assemble server side and not client side.

as to your specific issue you cannot automatically prepend a form value with a constant in the way that you are suggesting. form values must also be enquoted.

if you wish to manipulate a control's value before submit then you must do so with javascript. however as you are sending this to a php server doing so makes absolutely no sense at all. do all manipulations at the php side.

then once you have assembled the query string in php use header() to redirect the browser. something like this should work (not tested though).

Code:
<?php

if(isset($_POST['xSubmit'])):
	process();
else:
	displayForm();
endif;

function displayForm($msg =null, $ip = null){ 
	if($msg): 
?>
<div id="errorMessage">
	<?php echo htmlspecialchars($msg); ?>
</div>

<?php endif; ?>

<form action='index.php' method="post">
	<fieldset>
		Search IP address: <input type='text' name='target_ip' value="<?php echo $ip;?>"/><br/>
		<input type="radio" name="type" value="octal" checked="checked" /> &nbsp; Octal<br/>
		<input type="radio" name="type" value="decimal" /> &nbsp; Decimal <br/>
		<input type='hidden' name='-action' value='list' />
		<input type='submit' name='xSubmit' value='Search' />
	</fieldset>
</form>

<?php 
}
function fault($msg = null){
	displayForm($msg, trim($_POST['target_ip']));
	exit;
}
function process(){
	if(!isset($_POST['-action'])):
		fault("No action value supplied");
	endif;
	switch ($_POST['-action']):
		
		case 'list':
			$ip = trim($_POST['target_ip']);
			switch($_POST['type']):
				case 'octal':
					break;	
				case 'decimal':
					$ip = long2ip($ip);
					break;
			endswitch; 
			if(!preg_match('|^\d{1,3}\.\d{1,3}\.\d{1,3}$|', $ip)):
				//not an ipv4 address
				displayForm('Invalid IP Address');
			endif;
					
			//convert ip to decimal notation
			$ip = ip2long($ip);
			
			//assemble url
			$url = '[URL unfurl="true"]http://192.168.1.102/Integra/index.php?';[/URL]
			$params = array('-table'=>'FIREWALL_INTERFACES',
							'Lower_Dec'=>'<=' . $ip,
							'Upper_Dec'=>'>=' . $ip);
			$qp = array();
			foreach($params as $key=>$value):
				$qp[] = urlencode($key) . '=' . urlencode($value);
			endforeach;
			$url .= implode('&amp;', $qp);
			header('Location:'.$url);
			exit;
			break;
		default:
			fault('Improper action');
	endswitch;
}
 
typo at line 51. should be fault() not displayform()

Code:
<?php

if(isset($_POST['xSubmit'])):
	process();
else:
	displayForm();
endif;

function displayForm($msg =null, $ip = null){ 
	if($msg): 
?>
<div id="errorMessage">
	<?php echo htmlspecialchars($msg); ?>
</div>

<?php endif; ?>

<form action='index.php' method="post">
	<fieldset>
		Search IP address: <input type='text' name='target_ip' value="<?php echo $ip;?>"/><br/>
		<input type="radio" name="type" value="octal" checked="checked" /> &nbsp; Octal<br/>
		<input type="radio" name="type" value="decimal" /> &nbsp; Decimal <br/>
		<input type='hidden' name='-action' value='list' />
		<input type='submit' name='xSubmit' value='Search' />
	</fieldset>
</form>

<?php 
}
function fault($msg = null){
	displayForm($msg, trim($_POST['target_ip']));
	exit;
}
function process(){
	if(!isset($_POST['-action'])):
		fault("No action value supplied");
	endif;
	switch ($_POST['-action']):
		
		case 'list':
			$ip = trim($_POST['target_ip']);
			switch($_POST['type']):
				case 'octal':
					break;	
				case 'decimal':
					$ip = long2ip($ip);
					break;
			endswitch; 
			if(!preg_match('|^\d{1,3}\.\d{1,3}\.\d{1,3}$|', $ip)):
				//not an ipv4 address
				fault('Invalid IP Address');
			endif;
					
			//convert ip to decimal notation
			$ip = ip2long($ip);
			
			//assemble url
			$url = '[URL unfurl="true"]http://192.168.1.102/Integra/index.php?';[/URL]
			$params = array('-table'=>'FIREWALL_INTERFACES',
							'Lower_Dec'=>'<=' . $ip,
							'Upper_Dec'=>'>=' . $ip);
			$qp = array();
			foreach($params as $key=>$value):
				$qp[] = urlencode($key) . '=' . urlencode($value);
			endforeach;
			$url .= implode('&amp;', $qp);
			header('Location:'.$url);
			exit;
			break;
		default:
			fault('Improper action');
	endswitch;
}
 
Hi jpadie,

thanks very much for your detailed analysis of my issue. And you are perfectly correct that there is something wrong with this:


The reason is the =>= is messing up the URL and I wanted to break down the form into simpler components first before I finish it up with the ip_address becoming a variable and sending this to the form.

But I will test your code snippet above and will let you know how it goes.

Thanks for all the help.
 
instead of this section
Code:
foreach($params as $key=>$value):
                $qp[] = urlencode($key) . '=' . urlencode($value);
            endforeach;
            $url .= implode('&amp;', $qp);

you could also do this (which is a bit neater and potentially a bit faster).

Code:
$url .= http_build_query($params, '', '&amp;');

in your receiving script php will automatically decode the url parameters so you can reference them simply as $_GET['Lower_Dec'] etc
 
Hi......

I may have hit a major stumbling block. The restriction is
your entire code must be within a Class in my code as the whole thing is part of a template and if I break the template, everything else behaves unpredictably. Right now there are syntax errors everywhere as I try to adapt your code inside a function. How can I elegantly put your entire snippet inside a function called function block__before_bread_crumbs() {

Thanks very much.

Here is what I have so far:


Code:
<?

class tables_FIREWALL_INTERFACES {

        function block__before_body() {
                 echo "This is some text test<p>";
                 
        }

function block__before_bread_crumbs() {



# if(isset($_POST['xSubmit'])):
#    process();
#else:
#    displayForm();
#endif;

#function displayForm($msg =null, $ip = null){
#    if($msg):


#<div id="errorMessage">
#    <?php echo htmlspecialchars($msg); ?>
#</div>



 echo "<form action='index.php' method='post'>";
 echo    "<fieldset>";
 echo       "Search IP address: <input type='text' name='target_ip' value='<?php echo $ip;?>'/><br/>";
 echo       "<input type='radio' name='type' value='octal' checked='checked' /> &nbsp; Octal<br/>";
 echo       "<input type='radio' name='type' value='decimal' /> &nbsp; Decimal <br/>";
 echo       "<input type='hidden' name='-action' value='list' />";
 echo       "<input type='submit' name='xSubmit' value='Search' />";
 echo   "</fieldset>";
 echo "</form>";



function fault($msg = null){
    displayForm($msg, trim($_POST['target_ip']));
    exit;
}
function process(){
    if(!isset($_POST['-action'])):
        fault("No action value supplied");
    endif;
    switch ($_POST['-action']):
        
        case 'list':
            $ip = trim($_POST['target_ip']);
            switch($_POST['type']):
                case 'octal':
                    break;    
                case 'decimal':
                    $ip = long2ip($ip);
                    break;
            endswitch;
            if(!preg_match('|^\d{1,3}\.\d{1,3}\.\d{1,3}$|', $ip)):
                //not an ipv4 address
                fault('Invalid IP Address');
            endif;
                    
            //convert ip to decimal notation
            $ip = ip2long($ip);
            
            //assemble url
            $url = '[URL unfurl="true"]http://192.168.1.102/Integra/index.php?';[/URL]
            $params = array('-table'=>'FIREWALL_INTERFACES',
                            'Lower_Dec'=>'<=' . $ip,
                            'Upper_Dec'=>'>=' . $ip);
            $qp = array();
            foreach($params as $key=>$value):
                $qp[] = urlencode($key) . '=' . urlencode($value);
            endforeach;
            $url .= implode('&amp;', $qp);
            header('Location:'.$url);
            exit;
            break;
        default:
            fault('Improper action');
    endswitch;
    }
  
}
 
there is no reason why my code should cause syntax errors. so far as I am aware there is no syntax error present. perhaps the way you have edited it has introduced errors.

there MUST be some procedural code otherwise the class can never get engaged

Code:
<?php

class tables_Firewall_interfaces {
    
	
	/**
	 * public method to instantiate the process
	 * @return 
	 */
    public function block__before_bread_crumbs() {
        if (isset($_POST['xSubmit'])):
            $this->process();
		else:
			$this->displayForm();
        endif;
    }
    
	
	/**
	 * private method that will display the form with an error
	 * message (if required) and a sticky IP address value
	 * 
	 * @param object $msg [optional]
	 * @param object $ip [optional]
	 * @return 
	 */
    private function displayForm($msg = null, $ip = null) {
        if ($msg):
            
?>
<div id="errorMessage">
    <?php echo htmlspecialchars($msg); ?>
</div>
<?php endif; ?>
<form action='index.php' method="post">
    <fieldset>
        Search IP address: <input type='text' name='target_ip' value="<?php echo $ip;?>"/>
        <br/>
        <input type="radio" name="type" value="octal" checked="checked" />&nbsp; Octal
        <br/>
        <input type="radio" name="type" value="decimal" />&nbsp; Decimal 
        <br/>
        <input type='hidden' name='-action' value='list' /><input type='submit' name='xSubmit' value='Search' />
    </fieldset>
</form>
<?php 
	}
	
	/**
	 * private helper method to display the form with suitable error messages
	 * and then quit processing
	 * 
	 * @param object $msg [optional]
	 * @return 
	 */
	private function fault($msg = null){
		$this->displayForm($msg, trim($_POST['target_ip']));
		exit;
	}
	
	/**
	 * private method.  
	 * this does the heavy lifting of checking the variables are correctly formatted
	 * assembling the IP address and then 
	 * redirecting the browser
	 * 
	 * @return 
	 */
	private function process(){
		if(!isset($_POST['-action'])):
			$this->fault("No action value supplied");
		endif;
		switch ($_POST['-action']):
		
		case 'list':
			$ip = trim($_POST['target_ip']);
			switch($_POST['type']):
				case 'octal':
					break;	
				case 'decimal':
					$ip = long2ip($ip);
					break;
			endswitch; 
			if(!preg_match('|^\d{1,3}\.\d{1,3}\.\d{1,3}$|', $ip)):
				//not an ipv4 address
				fault('Invalid IP Address');
			endif;
					
			//convert ip to decimal notation
			$ip = ip2long($ip);
			
			//assemble url
			$url = '[URL unfurl="true"]http://192.168.1.102/Integra/index.php?';[/URL]
			$params = array('-table'=>'FIREWALL_INTERFACES',
							'Lower_Dec'=>'<=' . $ip,
							'Upper_Dec'=>'>=' . $ip);
			$qp = array();
			foreach($params as $key=>$value):
				$qp[] = urlencode($key) . '=' . urlencode($value);
			endforeach;
			$url .= implode('&amp;', $qp);
			header('Location:'.$url);
			exit;
			break;
		default:
			fault('Improper action');
	endswitch;
	}
}


//start the engine
$class = new tables_firewall_interfaces;
$class=>block__before_bread_crumbs();

?>
 
The last line should be
Code:
$class->block__before_bread_crumbs();
 
Hi jpadie,

First, thanks very much for the octal to decimal conversion and the error checking routines. You're above code now works correctly inside my FIREWALL_INTERFACES.php class.

If I fill in the form and choose 1.1.1.1 as the IP and choose octal, running the code now results in this URL:

Code:
[URL unfurl="true"]http://192.168.1.102/Integra/index.php?-table=FIREWALL_INTERFACES&amp;Lower_Dec=<%3D16843009&amp;Upper_Dec=>%3D16843009[/URL]

and the page doesn't display.

However, if I manually edit the above line by replacing the numberic codes with =<= and =>= like so:

Code:
[URL unfurl="true"]http://192.168.1.102/Integra/index.php?-table=FIREWALL_INTERFACES&Lower_Dec=<=167837956&Upper_Dec=>=167837956[/URL]

then the page displays perfectly.

something else is not obeying the urlencode function. From the link above in Firefox 3.6, I manually cut the URL as is and pasted it in IE and immediately after pasting, the URL converted itself to this:
Code:
[URL unfurl="true"]http://192.168.1.102/Integra-test/index.php?-table=FIREWALL_INTERFACES&Lower_Dec=%3C=167837956&Upper_Dec=%3E=167837956[/URL]
and it worked!

Next I then copied this URL back to firefox, and it mysteriously converted again back to
Code:
[URL unfurl="true"]http://192.168.1.102/Integra/index.php?-table=FIREWALL_INTERFACES&Lower_Dec=<=167837956&Upper_Dec=>=167837956[/URL]
and it worked again.

This makes me think that perhaps a little tweaking of your code to conver the resulting output from:
Code:
[URL unfurl="true"]http://192.168.1.102/Integra/index.php?-table=FIREWALL_INTERFACES&amp;Lower_Dec=<%3D16843009&amp;Upper_Dec=>%3D16843009[/URL]

to
Code:
[URL unfurl="true"]http://192.168.1.102/Integra-test/index.php?-table=FIREWALL_INTERFACES&Lower_Dec=%3C=167837956&Upper_Dec=%3E=167837956[/URL]

might just make everything work. Please ignore the fact the IP address was changing in my examples above. I was trying out different values.

Thanks very much.
 
Hi jpadie,

Great news! I have tested below

Code:
[URL unfurl="true"]http://192.168.1.102/Integra/index.php?-table=FIREWALL_INTERFACES&amp;Lower_Dec=%3C%3D167903498&amp;Upper_Dec=%3E%3D167903498[/URL]

doesn't work

Code:
[URL unfurl="true"]http://192.168.1.102/Integra/index.php?-table=FIREWALL_INTERFACES&Lower_Dec=%3C=167903498&Upper_Dec=%3E=167903498[/URL]

But this one does!

We're almost there. Would really really appreciate if you could help me finetune your script to remove the extraneous amp; characters.
 
change
Code:
$url .= implode('&amp;', $qp);
to
Code:
$url .= implode('&', $qp);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top