Jump to content

php charecter editor help / multiple forms


jeicrash

Recommended Posts

I am working on a char builder for a game and have run into a small problem.

The page contains two forms right now and will contain even more later.

When I submit one form it resets the value for the second form.

Is their a way to keep the form values after being submitted?

 

Here is my code so far

and this is not online yet I am using xampp to build it first.

and no session is being used.

 

<? 
$php_self = $_SERVER['PHP_SELF']; 
$normal = 'images/eyes.jpg';
$red = 'images/eyesred.jpg';
$blue = 'images/eyesblue.jpg';
$yellow = 'images/eyesyellow.jpg';
$lipsnorm = 'images/lips2.jpg';
$lipsgrey = 'images/lips2grey.jpg';
$lipsred = 'images/lips2red.jpg';
$eyes = $_POST['EyeColor'];
$lips = $_POST['Lips'];

if (!ISSET($_POST['EyeColor']) OR ($_POST['EyeColor'] == 'Normal')){
$eyes = $normal;
    }elseif	($_POST['EyeColor'] == 'Red') {
		$eyes = $red ;
	}elseif	($_POST['EyeColor'] == 'Blue') {
		$eyes = $blue ;
	}elseif	($_POST['EyeColor'] == 'Yellow') {
		$eyes = $yellow ;
		}
if (!ISSET($_POST['Lips']) OR ($_POST['Lips'] == 'Normal')) {
$lips = $lipsnorm;
}elseif ($_POST['Lips'] == 'Red') {
	$lips = $lipsred;
}elseif ($_POST['Lips'] == 'Grey') {
	$lips = $lipsgrey;
}

?>
<table border="1">
<tr>
<td>
<form id="form1" name="form1" method="post" action="<? $php_self ?>">
  <p>Eye Color:<br>
    <select name="EyeColor">
      <option>Normal</option>
      <option>Red</option>
      <option>Blue</option>
      <option>Yellow</option>
    </select><br>
    <label>
    <input type="submit" name="submit" id="submit" value="Set" />
    </label>
</p>
</form>
<form id="form2" name="form2" method="post" action="<? $php_self ?>">
<p>Lip Color:<br>
<select name="Lips">
      <option>Normal</option>
      <option>Red</option>
      <option>Grey</option>
    </select>  <br>
    <label>
    <input type="submit" name="submit" id="submit" value="Set" />
    </label>
</p>
</form>
</td>
</tr>

</table>
<? echo $lips . '<br>';
echo $eyes; ?>
<center>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td><img src="images/hair1.jpg"></td><td><img src="images/hair2.jpg"></td><td><img src="images/hair3.jpg"></td>
</tr>
<tr>
<td><img src="images/hair4.jpg"></td><td><img src="<? echo $eyes; ?>"></td><td><img src="images/hair5.jpg"></td>
</tr>
<tr>
<td colspan="3"><img src="images/nose.jpg"></td></tr>
<tr>
<td><img src="images/lips1.jpg"></td><td><img src="<? echo $lips; ?>"></td><td><img src="images/lips3.jpg"></td>
</tr>
<tr>
<td colspan="3"><img src="images/chin.jpg"></td>
</tr>
</table>
</center>
</body>

Link to comment
Share on other sites

You're going to have to either use ajax to post the forms, or pass the posted data back into the form after submission.  It's basically just having both forms act as 1, and adding a special name to each submit button, so your script will know which data to act on, so it can then put the other data into session variables. Then you have the post file redirect back to the form, and have it auto-fill the input="" variables of the form that wasn't submitted. Am I making any sense?

Link to comment
Share on other sites

$eyes = $_POST['EyeColor'];
$lips = $_POST['Lips'];

 

This part is suppose to hold the values after being posted.

If I make it all one form it works fine, but then you have to make all your choices at once. I'm not sure if somewhere I am un-knowingly un-setting the values.

 

I'm going to look around some more, there has to be a way to use the form ID in php.

 

Thanks again, if all else fails I may look into ajax, but i'd like to stay away from any extra languages for now.

Link to comment
Share on other sites

ok I figured out a work around. I did away with the multi-form layout and went with radio buttons with images next to them.

 

Now users simply choose their choices and click the preview button to see what the char. will look like.

 

This worked better for several reasons.

1. Its now very easy to add more items to the form

2. Less page hits while making the char.

3. No javascript or other languages needed

 

Now I get to start on creating some way to save it all to a database then they are done.

 

Thanks again.

Link to comment
Share on other sites

Here is my final code in case anyone else wants to do this type of thing.

 

<? 
$php_self = $_SERVER['PHP_SELF']; 
$normal = 'images/eyes.jpg';
$red = 'images/eyesred.jpg';
$blue = 'images/eyesblue.jpg';
$yellow = 'images/eyesyellow.jpg';
$lipsnorm = 'images/lips2.jpg';
$lipsgrey = 'images/lips2grey.jpg';
$lipsred = 'images/lips2red.jpg';
if (!ISSET($_POST['EyeColor']) OR ($_POST['EyeColor'] == 'Normal')){
$eyes = $normal;
    }elseif	($_POST['EyeColor'] == 'Red') {
		$eyes = $red ;
	}elseif	($_POST['EyeColor'] == 'Blue') {
		$eyes = $blue ;
	}elseif	($_POST['EyeColor'] == 'Yellow') {
		$eyes = $yellow ;
		}
if (!ISSET($_POST['Lips']) OR ($_POST['Lips'] == 'Normal')) {
$lips = $lipsnorm;
}elseif ($_POST['Lips'] == 'Red') {
	$lips = $lipsred;
}elseif ($_POST['Lips'] == 'Grey') {
	$lips = $lipsgrey;
}

?>
<table border="1">
<tr>
	<td>
<table border="1">
<tr>
<td>		
<form id="form1" name="form1" method="post" action="">
  <label> Eye Color<br />
  <input type="radio" name="EyeColor" id="EyeColor" value="Red" />
  <img src="images/eyesred.jpg" /></label>
  <p>
    <label>
    <input type="radio" name="EyeColor" id="EyeColor2" value="Blue" />
    <img src="images/eyesblue.jpg" /></label>
  </p>
  <p>
    <label>
    <input type="radio" name="EyeColor" id="EyeColor3" value="Yellow" />
    <img src="images/eyesyellow.jpg" /></label>
  </p>
  <?
if (ISSET ($_POST['EyeColor'])){
	echo '<p><label>Last selected<br>
    <input type="radio" name="EyeColor" id="EyeColor4" value="' . $_POST['EyeColor'] . '" checked="checked"/>
    <img src="' . $eyes . '" /></label></p>';
}
?>
  <p>Lips</p>
  <p>
    <label>
    <input type="radio" name="Lips" id="Lips" value="Grey" />
    <img src="images/lips2grey.jpg" /></label>
  </p>
  <p>
    <label>
    <input type="radio" name="Lips" id="Lips2" value="Red" />
    <img src="images/lips2red.jpg" /></label>
<br>
<label>
<p>
	<?
if (ISSET ($_POST['Lips'])){
	echo '<p><label>Last selected<br>
    <input type="radio" name="Lips" id="Lips3" value="' . $_POST['Lips'] . '" checked="checked"/>
    <img src="' . $lips . '" /></label></p>';
}
?>
    <input type="submit" name="submit" id="submit" value="Preview" />
    </label>
  </p>
</form>
</td>
</tr>

</table>
</td><td>
<center>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td><img src="images/hair1.jpg"></td><td><img src="images/hair2.jpg"></td><td><img src="images/hair3.jpg"></td>
</tr>
<tr>
<td><img src="images/hair4.jpg"></td><td><img src="<? echo $eyes; ?>"></td><td><img src="images/hair5.jpg"></td>
</tr>
<tr>
<td colspan="3"><img src="images/nose.jpg"></td></tr>
<tr>
<td><img src="images/lips1.jpg"></td><td><img src="<? echo $lips; ?>"></td><td><img src="images/lips3.jpg"></td>
</tr>
<tr>
<td colspan="3"><img src="images/chin.jpg"></td>
</tr>
</table>
</td>
</tr>
</table>
</center>
</body>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.