Hi,

I need to set a text feild value unchangable after a user enter first time. I dont want to use it's "readOnly" attribute as it let's the user to select the text.

Thanks
Regards,

Hi,

I need to set a text feild value unchangable after a user enter first time. I dont want to use it's "readOnly" attribute as it let's the user to select the text.

Thanks
Regards,

<input type="text" name="" disabled >

<input type="text" name="" disabled >

Thanks for your reply.

But the disabled attribute does not show the value. I want that the value should not be disappered after user clicks submit button and also the value should be unchangable.

Thanks for your reply. The entered value disappears when I click the button, even if I make it readonly. I need to make the text box value read only after first click.

readOnly .

Can you post your code ? You have to retain the value of the textbox. :)

<input type="text" name="ch1" disabled= "true" id="ch1"/>
<? 
if( $_GET["e"] == 3 || $_GET["e"] == 4) 
 {   
echo "<script>
     {
     document.getElementById('ch1').disabled = false; 
     document.getElementById('guess_value').readOnly = true;
     }
    </script>";
}

?>  
<input type="text" name="ch2" disabled="true" id="ch2"  />
<? 
if( $_GET["e"] == 5 || $_GET["e"] == 6) 
 {  // [B]Here I need to retain the value of first text box entered by user [/B]
echo "<script>
   {
 document.getElementById('ch2').disabled = false; 
 document.getElementById('ch1').disabled = false; 
document.getElementById('ch1').style.backgroundColor = '#FFFFCC';
 document.getElementById('ch1').readOnly = true;
 }
 </script>";

}
		
?>

Can you post your code ? You have to retain the value of the textbox. :)

I don't see element guess_value in this code! :) Well, here is an example.

<?php
$value = isset($_POST['name']) ? $_POST['name']:"10";
?>
<html>
<body>
<form method='post'>
<input type='text' name='name' value='<?php echo $value; ?>' readOnly='readOnly'>
<input type='submit' name='submit' value='submit'>
</form>
</body>
</html>

I have hard-coded value 10. You can replace it with whatever value (of the variable) you are printing in guess_value.

Thanks for your help. Can you please explain me this line:

$value = isset($_POST['name']) ? $_POST['name']:"10";

if I place $_POST instead of 10 then will it be correct?

or it can be possible as:

<input type="text" name="ch1" value = "<?echo  $_POST['ch1']; ?>" />

I don't see element guess_value in this code! :) Well, here is an example.

<?php
$value = isset($_POST['name']) ? $_POST['name']:"10";
?>
<html>
<body>
<form method='post'>
<input type='text' name='name' value='<?php echo $value; ?>' readOnly='readOnly'>
<input type='submit' name='submit' value='submit'>
</form>
</body>
</html>

I have hard-coded value 10. You can replace it with whatever value (of the variable) you are printing in guess_value.

$value = isset($_POST['name']) ? $_POST['name']:"10"; ? is called Ternary Operator. In the example above, I am checking if $_POST is set and has a value. If has a value, I am assigning it to the variable $value. If it doesn't have any value, I am assigning 10 to $value.
You can read more about ternary operator here.
http://in.php.net/operators.comparison

<input type="text" name="ch1" value = "<?echo $_POST; ?>" />

This will retain whatever value the user had entered in the textbox "ch1".

Edit: I don't recommend using <? (or short tags). Short tags are turned off on most servers, which would result in printing the php code to the screen. Use <?php instead.

Thanks for your help

$value = isset($_POST['name']) ? $_POST['name']:"10"; ? is called Ternary Operator. In the example above, I am checking if $_POST is set and has a value. If has a value, I am assigning it to the variable $value. If it doesn't have any value, I am assigning 10 to $value.
You can read more about ternary operator here.
http://in.php.net/operators.comparison

This will retain whatever value the user had entered in the textbox "ch1".

Edit: I don't recommend using <? (or short tags). Short tags are turned off on most servers, which would result in printing the php code to the screen. Use <?php instead.

You are welcome! :)

Can you please tell me what is the error in the following code, why it is not showing value in the text box.

<form name="game" action="gcheck.php" method="post">
<table align="center" width="71%">
  <tr>
    <td align="center"><h3>&nbsp;</h3> </td>
    <td width="33%" align="center">Computer think a number between 1-20</td>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td width="26%">Enter your guess here:</td>
    <td><input type="text" name="guess_value" id="guess_value"/></td>
<td><? 
switch ( $_GET["e"] )
{
	case 1:
		echo "You did not enter any number";
		break;
			
		case 2:
			
		echo "The number you entered is out of range";
		break;
		
}
?></td>
<td>&nbsp;</td>
  </tr>
	<tr>
	  <td>&nbsp;</td>
	  <td>&nbsp;</td>
	  <td>&nbsp;</td>
	  <td>&nbsp;</td>
    </tr>
	<tr>
	  <td>Chance 1: </td>
	  <td><label>

 <input type="text" name="ch1" disabled= "true" id="ch1" value= "<? echo $_POST['ch1'];  ?>" />
	<? 
	if( $_GET["e"] == 3 || $_GET["e"] == 4) 
	 {   
	echo "
	<script>
	{
	 document.getElementById('ch1').disabled = false; 
	 document.getElementById('guess_value').readOnly = true;
	 }
 </script>";
}
		
?>  
 </label></td>
  <td>
  <? 
   switch ( $_GET["e"] )
	{
	case 3:
		echo "Guessed no is less...You are given first chance";
			break;
	
	case 4:
			
		echo "Guessed no is greater...You are given first chance";
			break;

		}
  ?>
 
  </td>
  <td>&nbsp;</td>
    </tr>
<tr>
      <td>Chance 2: </td>
  <td><label>
        <input type="text" name="ch2" disabled="true" id="ch2"/>
	<? 
	if( $_GET["e"] == 5 || $_GET["e"] == 6) 
	 {   
	echo "
	<script>
	{
	 document.getElementById('ch2').disabled = false; 
	 document.getElementById('ch1').disabled = false; 
	 document.getElementById('ch1').style.backgroundColor = '#FFFFCC';
	 document.getElementById('ch1').readOnly = true;

 }
 </script>";
	 
}
	
?>  
   </label></td>
  <td>
  <? 
  
   switch ( $_GET["e"] )
	{
	case 5:
		echo "Guessed no is greater...You are given second chance";
		break;
		
	case 6:
		
		echo "Guessed no is less...You are given second chance";
		break;

	}
  ?>
	  
 </td>
  <td>&nbsp;</td>
    </tr>
<tr>
     <td>Chance 3: </td>
  <td><label>
       <input type="text" name="ch3" disabled="true" id="ch3"/>
	<? 
	if( $_GET["e"] == 7 || $_GET["e"] == 8) 
	 {   

	echo "
	<script>
	{
	 document.getElementById('ch3').disabled = false; 
	 document.getElementById('ch2').disabled = false; 
	 document.getElementById('ch1').disabled = false; 

	 }
	 </script>";
	}
		
	?>  
		
      </label></td>
	  <td>
	  
  <? 
  
   switch ( $_GET["e"] )
	{
	case 7:
		echo "Guessed no is greater...You are given third chance";
		break;
		
	case 8:
		
		echo "Guessed no is less...You are given third chance";
		break;

	}
  ?>
	  	  
  </td>
	  <td>&nbsp;</td>
    </tr>
	<tr>
	  <td>&nbsp;</td>
	  <td>&nbsp;</td>
	  <td>&nbsp;</td>
	  <td>&nbsp;</td>
    </tr>
	<tr>
	  <td>&nbsp;</td>
      <td>
	  <? 
	  
if( $_GET["e"] == 9) 
	{

	echo"
	<script>
	 document.getElementById('ch1').disabled = false; 
	 document.getElementById('ch2').disabled = false; 
	 document.getElementById('ch3').disabled = false; 
	 </script> ";		 

	echo "You lose the game. The guessed no was: "; 
	
	}
	  
  ?>
	  
  </td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
	</tr>
<tr>
  <td>&nbsp;</td>
  <td><input name="submit" type="submit" value="Guess" id="guess"/></td>
  <td width="41%">&nbsp;</td>
  <td width="41%">&nbsp;</td>
   </tr>
</table>

</form>

And the code for "gcheck.php" is as follows:

<?php

	$no = $_POST['guess_value'];
	$c1 = $_POST['ch1'];
	$c2 = $_POST['ch2'];
	$c3 = $_POST['ch3'];
	
	 
	if( $no == "" && $c1 == "" &&  $c2 == "" && $c3 == "")
	{
		header("location:guess.php?e=1");
		exit;
	
	}

	if( ($no < 1 || $no > 20) && $c1 == "" &&  $c2 == "" && $c3 == "")
	{
	
		header("location:guess.php?e=2");
		exit;
	}

	//$cmp_no = rand(1, 20);
	$cmp_no = 15;
	
	if( $cmp_no < $no && $c1 == "" &&  $c2 == "" && $c3 == "")
	{
	
		header("location:guess.php?e=3");
		//c1.attr("disabled", false);  

		exit;
	}
	
	if ( $cmp_no > $no && $c1 == "" &&  $c2 == "" && $c3 == "")
	{
		
		header("location:guess.php?e=4");
		//c1.attr("disabled", false);  
		exit;
	}
	
	// First choice checking
	

	
	if ( $cmp_no > $c1 && $no == "" &&  $c2 == "" && $c3 == "")
	{
		
		header("location:guess.php?e=5");
		exit;
	}
	
	if ( $cmp_no < $c1 && $no == "" &&  $c2 == "" && $c3 == "")
	{
		
		header("location:guess.php?e=6");
		exit;
	}

// 2nd choice checking
	

	
	if ( $cmp_no > $c2 && $no == "" &&  $c1 == "" && $c3 == "")
	{
		
		header("location:guess.php?e=7");
		exit;
	}
	
	if ( $cmp_no < $c2 && $no == "" &&  $c1 == "" && $c3 == "")
	{
		
		header("location:guess.php?e=8");
		exit;
	}
	
	
// 3rd choice checking
	
	if ( ($cmp_no > $c3 || $cmp_no < $c2) && $no == "" &&  $c1 == "" && $c2 == "")
	{
		
		header("location:guess.php?e=9");
		exit;
	}

	
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.