Jump to content

Saving text field input with tick box?


Dustin Lee
 Share

Recommended Posts

I have just started with coding, so bare with me please.I have a shout box that requires a Name and Comment to be entered, but I've been wanting it to save the Username upon the tick of a box so it wouldn't have to be re-entered, is it possible?I have tried google but it's not giving me the results I seek.I'd appreciate the help greatly!Here is the script, it seems basic enough, but I wouldn't have a clue as to where to start:

?php/* ###################### INSTALLATION ###################### */   // a) Adjust the configuration variables to your needs		 $file = "shouts.txt"; // Name of the file which							   // contains the shouts		 $shouts = 15; // Number of shouts to be displayed		 $maxlength_name = "20"; // Maximum length of name		 $maxlength_text = "150"; // Maximum length of text		 $break_name = "15"; // Break name after characters							 // without space		 $break_text = "15"; // Break text after characters							 // without space   //  Copy this code to your PHP file   // c) Copy your PHP file and the shouts file defined in   //	variable $file to your server using ASCII mode   // d) Make the shouts file writable (Windows: adjust   //	security, Unix: chmod 777)/* ###################### INSTALLATION ###################### *//* ############# SCRIPT (EDIT AT YOUR OWN RISK) ############# */?><body bgcolor="#606060"><p><STYLE TYPE="text/css">body {background-color:#606060;}body {color:white;}body {font-size: 12px;}body {font-family: 'Franklin Gothic Medium', sans-serif;}a:link {COLOR: #b1b1b1;}a:visited {COLOR: #b1b1b1;}a:hover {COLOR: #838383;}a:active {COLOR: #838383;}p {line-height:100%;}#button {width: 50px;height: 25px;border: none;background: url(/button.jpg) no-repeat left top;color: #92d3eb;}</STYLE><form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">	<input type="text" value="Your name" name="input_name" maxlength="<?php echo $maxlength_name; ?>" onfocus="if(this.value=='Your name'){this.value='';}" onblur="if(this.value==''){this.value='Your name';}" /><br />	<input type="text" value="Your text" name="input_text" maxlength="<?php echo $maxlength_text; ?>" onfocus="if(this.value=='Your text'){this.value='';}" onblur="if(this.value==''){this.value='Your text';}" /><br />	<input type="submit" id='button' value="Send" /><FORM METHOD="LINK" ACTION="chat.php"><INPUT id='button' TYPE="submit" VALUE="Refresh"></FORM>  </form></p><hr /><p></body><?php  //function to break text after x characters  function str_break($str,$maxlen){	$nobr = 0;	$len = strlen($str);	for($i=0;$i<$len;$i++){	  if(($str[$i]!=' ') && ($str[$i]!='-') && ($str[$i]!="\n")){		$nobr++;	  }else{		$nobr = 0;		if($maxlen+$i>$len){		  $str_br .= substr($str,$i);		  break;		}	  }	  	  if($nobr>$maxlen){		$str_br .= ' '.$str[$i];		$nobr = 1;	  }else{		$str_br .= $str[$i];	  }	}		return $str_br;  }  //number of shouts to be displayed  $display = (isset($_GET["show"]) ? "all" : $shouts);  //print links to either show all or specific number of shouts  if($display == "all"){	?><a href="<?php echo $_SERVER["PHP_SELF"]; ?>">View small shoutbox</a><?php  }else{	?><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?show=all">View all shouts</a><?php  }?></p><p><?php  //insert new shout  $input_name = $_POST["input_name"];  $input_text = $_POST["input_text"];    //check if form has been submitted  if(isset($input_name) && isset($input_text) && $input_name!="Your name" && $input_text!="Your text" && strlen($input_name)>0 && strlen($input_text)>0){	//get last name and comment	$handle = fopen($file,"r");	while(!feof($handle)){	  $row = fgets($handle,999999);	  list($tmp_name,$tmp_text) = split("\|\|\|\|\|",$row);	  if($tmp_name != "" && $tmp_text != ""){		$last_name = $tmp_name;		$last_text = str_replace("\n","",$tmp_text);	  }	}		fclose($handle);	$input_name = str_break($input_name,$break_name); //break name	$input_name = str_replace("<","<",$input_name); //prevent html input	$input_name = str_replace(">",">",$input_name); //prevent html input	$input_name = stripslashes($input_name); //strip slashes		$input_text = str_break($input_text,$break_text); //break text	$input_text = str_replace("<","<",$input_text); //prevent html input	$input_text = str_replace(">",">",$input_text); //prevent html input	$input_text = stripslashes($input_text); //strip slashes	if($last_name != $input_name || $last_text != $input_text){	  $handle = fopen($file,"a"); //open shouts file to write (append)	  fputs($handle,"$input_name|||||$input_text\n"); //insert name and shout	  fclose($handle); //close file handle	}  }  //read shouts file  $names = array(); //array to store names  $shouts = array(); //array to store shouts  $handle = fopen($file,"r"); //open shouts file to read    while(!feof($handle)){ //read row by row	$row = fgets($handle,999999);	list($name,$shout) = split("\|\|\|\|\|",$row);		if($name){	  array_push($names,$name);	  array_push($shouts,$shout);	}  }    fclose($handle); //close file handle  //reverse arrays so that new lines are first  $names = array_reverse($names);  $shouts = array_reverse($shouts);  //number of shouts to really print  $max = ($display == "all" ? count($names) : $display);  //print shouts  for($i=0;$i<$max && $i<count($names);$i++){	?><p><strong><?php echo $names[$i]; ?>:</strong> <?php echo $shouts[$i]; ?></p>  <?php } ?></p>

I'm not sure of the origin of this script, someone emailed it to me.

Edited by Dustin Lee
Link to comment
Share on other sites

Is this the contents of chat.php? If it is you can do the following:Add your checkbox input somewhere in this area, either before or after the 'input_name' text box depending on where you want it:<input type="text" value="Your name" name="input_name" ... /><br /><input type="text" value="Your text" name="input_text" ... /><br /><input type="submit" id='button' value="Send" />Add an onclick handler to the submit button which will set the value of your checkbox to the value of the name input.Then check a $_POST variable when you print out your inputs:

<?php$postName = 'Your name';if (!empty($_POST['saveName_check'])) {   $postName = $_POST['saveName_check'];}echo "<input type='text' value='".$postName."' name='input_name' ... /><br />\n";?><input type="text" value="Your text" name="input_text" ... /><br /><input type="submit" id='button' value="Send" />

Link to comment
Share on other sites

I must be a bigger newb at coding than I thought, because I hardly understood what you just said.PHP is still very new to me (meaning I haven't even started looking at it).I need to crack down and get to learnin' PHP, it seems like most of everything is done in the language.

Edited by Dustin Lee
Link to comment
Share on other sites

Lots of the mechanics behind complex websites is implemented through the use of server-side programming. PHP is a popular server-side scripting language, however it is not the only one, and you can learn one of the other options (e.g. ASP.NET, Ruby, C++) instead if you feel like it.

Link to comment
Share on other sites

I must be a bigger newb at coding than I thought, because I hardly understood what you just said.PHP is still very new to me (meaning I haven't even started looking at it).I need to crack down and get to learnin' PHP, it seems like most of everything is done in the language.
Well, let's take this a little bit at a time then. Add the checkbox input to your code:<input type='checkbox' name='saveName_check' id='saveName_check' value='' /><label for='saveName_check'>Save name</label><input type="text" value="Your name" name="input_name" ... /><br /><input type="text" value="Your text" name="input_text" ... /><br /><input type="submit" id='button' value="Send" />So we have the checkbox. Now let's add the onclick handler for the submit button:<input type='submit' id='button' value='Send' onclick='setCheckboxValue();' />Now create the function in JavaScript. Put this in the <head> of your document between <script> tags:
function setCheckboxValue() {   var chkSaveName = document.getElementById("saveName_check");   var txtName = document.getElementById("input_name");      if (chkSaveName.checked) {	  chkSaveName.value = txtName.value;   }}

You'll also need to give an id to your name input box:<input type="text" value="Your name" name="input_name" id="input_name" ... /><br />Once you get this done, we'll start with the PHP.

Link to comment
Share on other sites

So with all that done, it will look something like this?

<html><title>Chat</title><script>function setCheckboxValue() {var chkSaveName = document.getElementById("saveName_check");var txtName = document.getElementById("input_name");if (chkSaveName.checked) {chkSaveName.value = txtName.value;}}</script></head><body><form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post"><input type="text" value="Your name" name="input_name" id="input_name" maxlength="<?php echo $maxlength_name; ?>" onfocus="if(this.value=='Your name'){this.value='';}" onblur="if(this.value==''){this.value='Your name';}" /><input type='checkbox' name='saveName_check' id='saveName_check' value='' /><label for='saveName_check'>Save name</label><br /><input type="text" value="Your text" name="input_text" maxlength="<?php echo $maxlength_text; ?>" onfocus="if(this.value=='Your text'){this.value='';}" onblur="if(this.value==''){this.value='Your text';}" /><br /><input type="submit" id='button' value="Send" onclick='setCheckboxValue();' /><FORM METHOD="LINK" ACTION="chat.php"><INPUT id='button' TYPE="submit" VALUE="Refresh"></FORM></body></html> 

Javascript :) This seems to be getting more and more advanced as it goes a long. I thought at first this would be just HTML, then turns into PHP, then into JavaScript!

Link to comment
Share on other sites

how webpages become more than just static text and images. It might seem daunting at first, but overtime, the more you see it and the more you read about it, and more importantly the more you work with it, the easier it becomes (just like most things.) Once you get it working for you though, all of a sudden it become pretty darn cool. At least that's how it worked for me anyway. :)

Edited by thescientist
Link to comment
Share on other sites

So with all that done, it will look something like this?
Yes, that looks correct. Now what I need to know is do you have two separate files or is everything in one file? More specifically, is the code you just posted part of chat.php or is chat.php a completely different file?
Javascript :) This seems to be getting more and more advanced as it goes a long. I thought at first this would be just HTML, then turns into PHP, then into JavaScript!
Don't worry. It'll get easier. :) Besides, you've got the forums to help you! :)
Link to comment
Share on other sites

Don't worry. It'll get easier. :) Besides, you've got the forums to help you! :)
I started looking at W3's tutorials last night before I had to go out, it's helping out all ready! (:
Yes, that looks correct. Now what I need to know is do you have two separate files or is everything in one file? More specifically, is the code you just posted part of chat.php or is chat.php a completely different file?
The code I posted is part of chat.php
<?php/* ###################### INSTALLATION ###################### */   // a) Adjust the configuration variables to your needs         $file = "shouts.txt"; // Name of the file which                               // contains the shouts         $shouts = 15; // Number of shouts to be displayed         $maxlength_name = "20"; // Maximum length of name         $maxlength_text = "150"; // Maximum length of text         $break_name = "15"; // Break name after characters                             // without space         $break_text = "15"; // Break text after characters                             // without space   //  Copy this code to your PHP file   // c) Copy your PHP file and the shouts file defined in   //    variable $file to your server using ASCII mode   // d) Make the shouts file writable (Windows: adjust   //    security, Unix: chmod 777)/* ###################### INSTALLATION ###################### *//* ############# SCRIPT (EDIT AT YOUR OWN RISK) ############# */?><head><title>CHAT</title><script>function setCheckboxValue() {var chkSaveName = document.getElementById("saveName_check");var txtName = document.getElementById("input_name");if (chkSaveName.checked) {chkSaveName.value = txtName.value;}}</script></head><body bgcolor="#606060"><p><form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post"><input type="text" value="Your name" name="input_name" id="input_name" maxlength="<?php echo $maxlength_name; ?>" onfocus="if(this.value=='Your name'){this.value='';}" onblur="if(this.value==''){this.value='Your name';}" /><input type='checkbox' name='saveName_check' id='saveName_check' value='' /><label for='saveName_check'>Save name</label><br /><input type="text" value="Your text" name="input_text" maxlength="<?php echo $maxlength_text; ?>" onfocus="if(this.value=='Your text'){this.value='';}" onblur="if(this.value==''){this.value='Your text';}" /><br /><input type="submit" id='button' value="Send" onclick='setCheckboxValue();' /><FORM METHOD="LINK" ACTION="chat.php"><INPUT id='button' TYPE="submit" VALUE="Refresh"></FORM>  </form></p><hr /><p></body><?php  //function to break text after x characters  function str_break($str,$maxlen){    $nobr = 0;    $len = strlen($str);    for($i=0;$i<$len;$i++){      if(($str[$i]!=' ') && ($str[$i]!='-') && ($str[$i]!="\n")){        $nobr++;      }else{        $nobr = 0;        if($maxlen+$i>$len){          $str_br .= substr($str,$i);          break;        }      }            if($nobr>$maxlen){        $str_br .= ' '.$str[$i];        $nobr = 1;      }else{        $str_br .= $str[$i];      }    }        return $str_br;  }  //number of shouts to be displayed  $display = (isset($_GET["show"]) ? "all" : $shouts);  //print links to either show all or specific number of shouts  if($display == "all"){    ?><a href="<?php echo $_SERVER["PHP_SELF"]; ?>">View small shoutbox</a><?php  }else{    ?><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?show=all">View all shouts</a><?php  }?></p><p><?php  //insert new shout  $input_name = $_POST["input_name"];  $input_text = $_POST["input_text"];    //check if form has been submitted  if(isset($input_name) && isset($input_text) && $input_name!="Your name" && $input_text!="Your text" && strlen($input_name)>0 && strlen($input_text)>0){    //get last name and comment    $handle = fopen($file,"r");    while(!feof($handle)){      $row = fgets($handle,999999);      list($tmp_name,$tmp_text) = split("\|\|\|\|\|",$row);      if($tmp_name != "" && $tmp_text != ""){        $last_name = $tmp_name;        $last_text = str_replace("\n","",$tmp_text);      }    }        fclose($handle);    $input_name = str_break($input_name,$break_name); //break name    $input_name = str_replace("<","<",$input_name); //prevent html input    $input_name = str_replace(">",">",$input_name); //prevent html input    $input_name = stripslashes($input_name); //strip slashes        $input_text = str_break($input_text,$break_text); //break text    $input_text = str_replace("<","<",$input_text); //prevent html input    $input_text = str_replace(">",">",$input_text); //prevent html input    $input_text = stripslashes($input_text); //strip slashes    if($last_name != $input_name || $last_text != $input_text){      $handle = fopen($file,"a"); //open shouts file to write (append)      fputs($handle,"$input_name|||||$input_text\n"); //insert name and shout      fclose($handle); //close file handle    }  }  //read shouts file  $names = array(); //array to store names  $shouts = array(); //array to store shouts  $handle = fopen($file,"r"); //open shouts file to read    while(!feof($handle)){ //read row by row    $row = fgets($handle,999999);    list($name,$shout) = split("\|\|\|\|\|",$row);        if($name){      array_push($names,$name);      array_push($shouts,$shout);    }  }    fclose($handle); //close file handle  //reverse arrays so that new lines are first  $names = array_reverse($names);  $shouts = array_reverse($shouts);  //number of shouts to really print  $max = ($display == "all" ? count($names) : $display);  //print shouts  for($i=0;$i<$max && $i<count($names);$i++){    ?><p><strong><?php echo $names[$i]; ?>:</strong> <?php echo $shouts[$i]; ?></p>  <?php } ?></p>

Edited by Dustin Lee
Link to comment
Share on other sites

I started looking at W3's tutorials last night before I had to go out, it's helping out all ready! (:
Great! :)
The code I posted is part of chat.php
Ok, then all you need to do:When you print this line:<input type="text" value="Your name" name="input_name" id="input_name" ... />Print it like this instead:
<?php$postName = 'Your name';if (!empty($_POST['saveName_check'])) {   $postName = $_POST['saveName_check'];}echo "<input type='text' value='".$postName."' name='input_name' ... /><br />\n";?>

If chat.php is loaded the first time or the checkbox is not checked then $_POST['saveName_check'] will not be set and $postName will equal 'Your name' otherwise $postName (and the value of the input) will be whatever value was put in the checkbox's value through the JavaScript function I gave you.Edit:I just noticed something with your HTML. You're missing an opening <head> tag. And your opening <script> tag should have the type attribute specified for it:<script type='text/javascript'>

Edited by jkloth
Link to comment
Share on other sites

Great! :)Ok, then all you need to do:When you print this line:<input type="text" value="Your name" name="input_name" id="input_name" ... />Print it like this instead:
<?php$postName = 'Your name';if (!empty($_POST['saveName_check'])) {   $postName = $_POST['saveName_check'];}echo "<input type='text' value='".$postName."' name='input_name' ... /><br />\n";?>

If chat.php is loaded the first time or the checkbox is not checked then $_POST['saveName_check'] will not be set and $postName will equal 'Your name' otherwise $postName (and the value of the input) will be whatever value was put in the checkbox's value through the JavaScript function I gave you.Edit:I just noticed something with your HTML. You're missing an opening <head> tag. And your opening <script> tag should have the type attribute specified for it:<script type='text/javascript'>

That works great, thanks a lot!I'll use this code to help me understand the language a bit more as well, this really helped :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...