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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

change text value 2

Status
Not open for further replies.

ruler1

MIS
Feb 19, 2007
89
hi peeps im having a small problem with changing the value of a input box using php.

in my html code i have this which i think is java

Code:
document.myform.box1.value=''

is there a way to do the same thing in php? i am trying to change the value of box1 so its the same as box2 if nothing is entered in box1 on submit. hope this makes some sence.
 
What you have there is javascript (and not really good javascript either), not java. The two share nothing but the first four letters in their name.

If you're asking to change the value of an input between the time user interacts with the page and the page is submitted, that cannot be done with PHP. Because PHP runs on the server, meaning that it works between the time user pushed submit and the new page was served to the user.

You can either continue doing that with javascript, which can co-exist perfectly with PHP (one is client-side the other server-side technology). In that case I suggest you head over to forum216 for more help on that.

Or you can change the processing PHP form so that if box1 comes through the form empty, then the value of box2 is used. This technique will also be more secure, since users can turn off their javascript, but they can't interfere with the PHP process.

___________________________________________________________
[small]Do something about world cancer today: Comprehensive cancer control information at PACT[/small]
 
i understand what you are saying and thanks for your reply.
the reason for changing the value of box2 is because the php script i am using refers to it several times. i will attemt to explain how it is supposed to work.

when someone clicks submit the php script would check the contents of box2, if it is empty then box2 would be set to the same value as box1 so it can be referred to by the php script like a variable.

i know it would be easier to just put them into variables from the start but it would mean having to rewrite a pretty huge php script so i was just wondering if there was a more simple way of doing this. after a lot of thinking i may have to rewrite the script anyway but it was worth asking first anyway. thanks again for your help.
 
Not sure what you are getting at, but something as simple as:

Code:
if(!isset($_POST['box2'])||empty($_POST['box2'])){
$_POST['box2']=$_POST['box1'];
}

Would make box2 have the value of box1 if box2 was empty or not submitted at all.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
the script i have written is for a global banlist for the p2p community. so many people have complained about pedophiles sharing child porn or looking for such files that i felt compelled to do something so i wrote a global IP banlist that can be used with PG2 or protowall and that IPs can be submitted through my site.

maybe if i posted here the chunk of code i am working with it might help? though its a little big. The code works great but i am trying to to make it work more easily for the people submitting IPs. there are 8 input box's, 4 for the start IP and 4 for the end IP but problem is that when someone wants to add a fixed IP for example 22.222.22.2 they will have to add 22.222.22.2 as the start IP and then 22.222.22.2 as the end IP. i am trying to make it so that if a user is entering a fixed IP they will only need to add it in the start IP and on submit it will automatically add the same IP to the end IP box's. If it is a range for example start IP 22.222.22.0 and end IP 22.222.22.255 it will not copy the start IP to the end IP box's. hope this makes sence but its to save on all the extra typing for them. here is the code i am working with below...

Code:
	if(isset($_REQUEST["subm1"]))
	{
		if ( !$userdata['session_logged_in'] )
		{
			echo '<meta http-equiv="refresh" content="1;url=login.php">';
			message_die(GENERAL_MESSAGE, "Session Expired, Please Login<br /><br /><img src=images/progbar.gif height=13 width=200 />");
		}
		$error_msg='';
		if(trim($HTTP_POST_VARS['ip5']) == '' && trim($HTTP_POST_VARS['ip6']) == '' && trim($HTTP_POST_VARS['ip7']) == '' && trim($HTTP_POST_VARS['ip8']) == '')
		{
			//put ip1 into ip5
			//put ip2 into ip6
			//put ip3 into ip7
			//put ip4 into ip8
		}
		if (!preg_match('/^[0-9]+$/' , trim($HTTP_POST_VARS['ip8'])) || trim($HTTP_POST_VARS['ip8']) > 255){$error_msg='<br />' . $lang['Err_End_Box'] . '4'.$error_msg;} else { $ip8 = trim($HTTP_POST_VARS['ip8']); $ip8 = $ip8 + 0;}
		if (!preg_match('/^[0-9]+$/' , trim($HTTP_POST_VARS['ip7'])) || trim($HTTP_POST_VARS['ip7']) > 255){$error_msg='<br />' . $lang['Err_End_Box'] . '3'.$error_msg;} else { $ip7 = trim($HTTP_POST_VARS['ip7']); $ip7 = $ip7 + 0;}
		if (!preg_match('/^[0-9]+$/' , trim($HTTP_POST_VARS['ip6'])) || trim($HTTP_POST_VARS['ip6']) > 255){$error_msg='<br />' . $lang['Err_End_Box'] . '2'.$error_msg;} else { $ip6 = trim($HTTP_POST_VARS['ip6']); $ip6 = $ip6 + 0;}
		if (!preg_match('/^[0-9]+$/' , trim($HTTP_POST_VARS['ip5'])) || trim($HTTP_POST_VARS['ip5']) > 255){$error_msg='<br />' . $lang['Err_End_Box'] . '1'.$error_msg;} else { $ip5 = trim($HTTP_POST_VARS['ip5']); $ip5 = $ip5 + 0;}
		if (!preg_match('/^[0-9]+$/' , trim($HTTP_POST_VARS['ip4'])) || trim($HTTP_POST_VARS['ip4']) > 255){$error_msg='<br />' . $lang['Err_Start_Box'] . '4'.$error_msg;} else { $ip4 = trim($HTTP_POST_VARS['ip4']); $ip4 = $ip4 + 0;}
		if (!preg_match('/^[0-9]+$/' , trim($HTTP_POST_VARS['ip3'])) || trim($HTTP_POST_VARS['ip3']) > 255){$error_msg='<br />' . $lang['Err_Start_Box'] . '3'.$error_msg;} else { $ip3 = trim($HTTP_POST_VARS['ip3']); $ip3 = $ip3 + 0;}
		if (!preg_match('/^[0-9]+$/' , trim($HTTP_POST_VARS['ip2'])) || trim($HTTP_POST_VARS['ip2']) > 255){$error_msg='<br />' . $lang['Err_Start_Box'] . '2'.$error_msg;} else { $ip2 = trim($HTTP_POST_VARS['ip2']); $ip2 = $ip2 + 0;}
		if (!preg_match('/^[0-9]+$/' , trim($HTTP_POST_VARS['ip1'])) || trim($HTTP_POST_VARS['ip1']) > 255){$error_msg='<br />' . $lang['Err_Start_Box'] . '1'.$error_msg;} else { $ip1 = trim($HTTP_POST_VARS['ip1']); $ip1 = $ip1 + 0;}
		if ($error_msg != '')
		{
			echo '<meta http-equiv="refresh" content="5;url=banlist_moderate.php">';
			message_die(GENERAL_MESSAGE, "<span class='rpostbody'>" . $error_msg . "</span><br /><br />You Are Being Redirected Back, Please wait<br /><br /><img src=images/progbar.gif height=13 width=200 />");
		}
		else
		{
			$reason = $_REQUEST['reason1'];
		 	if($reason == '1')
		 	{
				echo '<meta http-equiv="refresh" content="5;url=banlist_moderate.php">';
		 		message_die(GENERAL_MESSAGE, "<span class='rpostbody'>Error, You Forgot To Select A Reason.</span><br /><br />You Are Being Redirected Back, Please wait<br /><br /><img src=images/progbar.gif height=13 width=200 />");
		 	}

			$file = 'c:banlists/pedophiles-list.p2p';
			$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
			$lines = implode ("\r\n", $lines); //turn in to string
			$pattern = '/(?<info>.*?):(?<startIP>\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3})-(?<endIP>\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3})/ims';
			preg_match_all($pattern, $lines, $matches);
			array_walk($matches['info'], 'trim');

			$cc=0; 
			$enterIPstart = $ip1*256*256*256+$ip2*256*256+$ip3*256+$ip4+0;
			$enterIPend = $ip5*256*256*256+$ip6*256*256+$ip7*256+$ip8+0;

			while($matches['startIP'][$cc])
			{
				if(ereg("([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})", $matches['startIP'][$cc],$regs) && ereg("([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})", $matches['endIP'][$cc],$regs1))
				{
					$foundIPstart = $regs[1]*256*256*256+$regs[2]*256*256+$regs[3]*256+$regs[4]+0;
					$foundIPend = $regs1[1]*256*256*256+$regs1[2]*256*256+$regs1[3]*256+$regs1[4]+0;
				}
				// check to see if the end ip is lower than the start ip
				if ($enterIPstart > $enterIPend)
				{
					echo '<meta http-equiv="refresh" content="5;url=banlist_moderate.php">';
		 			message_die(GENERAL_MESSAGE, "<span class='rpostbody'>Error, End IP cant be lower than the Start IP.</span><br /><br />You Are Being Redirected Back, Please wait<br /><br /><img src=images/progbar.gif height=13 width=200 />");
				}
				//check to see if the IP ot a range exists first within the entered range
				if ($enterIPstart >= $foundIPstart && $enterIPstart <= $foundIPend || $enterIPend <= $foundIPend && $enterIPend >= $foundIPstart)
				{
		 			message_die(GENERAL_MESSAGE, "<span class='rpostbody'>Error, The IP you entered falls within this already banned range. Start IP: ". $matches['startIP'][$cc] ." End IP ". $matches['endIP'][$cc] ."</span><br /><br />Please Click Back!");
				}
				$cc++;
			}
			$fa = fopen("c:banlists/pedophiles-list.p2p", "a");
			if(! $fa)
			{
				echo '<meta http-equiv="refresh" content="5;url=banlist_moderate.php">';
				message_die(GENERAL_MESSAGE, "<span class='rpostbody'>Error, Failed to find file pedophiles-list.p2p</span><br /><br />You Are Being Redirected Back, Please wait<br /><br /><img src=images/progbar.gif height=13 width=200 />");
			}			
			fwrite($fa, $_REQUEST['reason1'] . ":" . $ip1 . "." . $ip2 . "." . $ip3 . "." . $ip4 . "-" . $ip5 . "." . $ip6 . "." . $ip7 . "." . $ip8 . "\r\n");
			fclose($fa);
			echo '<meta http-equiv="refresh" content="1;url=banlist_moderate.php">';
			message_die(GENERAL_MESSAGE, "<span class='rpostbody'>IP Range Added Successfully!</span><br /><br />You Are Being Redirected Back, Please wait<br /><br /><img src=images/progbar.gif height=13 width=200 />");
		}
	}
 
First $HTTP_POST_VARS is deprecated so there's not guarantee that your code will work with future versions of PHP
So I suggest you start using the $_POST variable instead.

However my solution then applies exactly to what you want to do.

Just change $_POST['..'] to the variables you want to check.
I'm assuming it would be $HTTP_POST_VARS['ip2']

So the code would look like:
Code:
if(!isset($HTTP_POST_VARS['ip2'])||empty($HTTP_POST_VARS['ip2'])){
$HTTP_POST_VARS['ip2']=$HTTP_POST_VARS['ip1'];
}

Before doing all the preg matches and stuff you seem to be doing there.




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
ok thanks i will give that a try and see how it goes. i will post back a bit later and let you know if it was a success :)
thanks again for the help much appreciated
 
that worked a charm its all working now 100% :)
thanks for the help i am very greatful.
 
Glad it worked for you.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top