Jump to content

Recommended Posts

I am trying to set a variable value through a form.  A user selects a color and the form sets the value of the variable. 

 

Maybe I am way off on how to set it up.  Here is what I have tried:

 

Form

<h4>Choose Color</h4>
<select name="color">
<option value="<?php $black ?>">Black</option>
<option value="<?php $white ?>">White</option>
<option value="<?php $grey ?>">Gray</option>
<option value="<?php $limegreen ?>">Limegreen</option>
</select>

 

And the PHP I am trying to execute:

$color  = $_POST['color'];

$black = imagecolorallocate($bg, 0, 0, 0);
$white = imagecolorallocate($bg, 255, 255, 255);
$grey = imagecolorallocate($bg, 128, 128, 128);
$limegreen = imagecolorallocate($bg, 194, 247, 0);

imagettftext($bg, $fontsize, 0, $x, $y, $color, $font, $text );

 

I am just trying to set the "$color" variable I have all the others working.  I still have much to learn so I may be totally out on my approach.

Link to comment
https://forums.phpfreaks.com/topic/206905-help-with-forms-and-php/
Share on other sites

Well first you're not echo'ing the following variables correctly

<option value="<?php $black ?>">Black</option>
<option value="<?php $white ?>">White</option>
<option value="<?php $grey ?>">Gray</option>
<option value="<?php $limegreen ?>">Limegreen</option>

You need to place echo before each variable otherwise nothing will be outputted and your form will submit blank values. I am assuming you have defined values for those variables too?

 

Secondly make sure you have wrapped your form items within <form action="path/to/proccessing/page.php" method="post"></form> tags and that you have defined a submit button, eg

<input type="submit" name="submit" value="Submit" />

I believe the form part is correct:

 

<form action="text2png2.php" method="post"> 		  

<h4>Choose desired font</h4>
<select name="font"> 
<option value="fonts/aerosol.ttf">Aerosol</option>
<option value="fonts/amsterdam.ttf">Amsterdam</option>
<option value="fonts/degrassi.ttf">Degrassi</option>
<option value="fonts/GRAFFITI.ttf">GRAFFITI</option>
<option value="fonts/bubble.ttf">Bubble</option>
<option value="fonts/3d-drop.ttf">3D Drop</option>
<option value="fonts/atomic-bomb.ttf">Atomic-Bomb</option>
<option value="fonts/graffonti.ttf">Graffonti</option>
<option value="fonts/juice.ttf">Juice</option>
<option value="fonts/mostwasted.ttf">Most Wasted</option>
<option value="fonts/nosed.ttf">Nosed</option>
<option value="fonts/street-soul.ttf">Street Soul</option>
<option value="fonts/stylin.ttf">Stylin</option>
</select>

<h4>Choose Color</h4>
<select name="color">
<option value="<?php echo $black ?>">Black</option>
<option value="<?php echo $white ?>">White</option>
<option value="<?php echo $grey ?>">Gray</option>
<option value="<?php echo $limegreen ?>">Pink</option>
</select>

<h4>Font Size</h4>
<select name="size" value="65" default="65">
<option value="25">25</option>
<option value="35">35</option>
<option value="45">45</option>
<option value="55">55</option>
<option value="65" selected>65</option>
<option value="75">75</option>
<option value="85">85</option>
</select>

<h4>Enter Text</h4> 

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


<input type="submit" name="submit" value="Create!!"/>
</form>

 

I thought I was defining the variables here:

$color = $_POST['color'];
$black = imagecolorallocate($bg, 0, 0, 0);
$white = imagecolorallocate($bg, 255, 255, 255);
$grey = imagecolorallocate($bg, 128, 128, 128);
$limegreen = imagecolorallocate($bg, 194, 247, 0);

 

I take it I am missing something here...

 

Umm. Yes your form part is sort of correct. I guess the variables $black, white, $grey and $limegreen are referring to these variables

$black = imagecolorallocate($bg, 0, 0, 0);
$white = imagecolorallocate($bg, 255, 255, 255);
$grey = imagecolorallocate($bg, 128, 128, 128);
$limegreen = imagecolorallocate($bg, 194, 247, 0);

 

imagecolorallocate does not return anything the colors will be stored in memory. So you want be able to insert these colors into your form. Instead what I'd do is change your color selection to

<h4>Choose Color</h4>
<select name="color">
<option value="black">Black</option>
<option value="white>">White</option>
<option value="grey">Gray</option>
<option value="limegreen">Limegreen</option>
</select>

 

Now in your form processing code when you retrieve the colour you'd do

$black = imagecolorallocate($bg, 0, 0, 0);
$white = imagecolorallocate($bg, 255, 255, 255);
$grey = imagecolorallocate($bg, 128, 128, 128);
$limegreen = imagecolorallocate($bg, 194, 247, 0);

switch(strtolower($_POST['color']))
{
     case 'black':
     case 'white':
     case 'grey':
     case 'limegreen':
        $color = ${$_POST['color']};
     break;
}

 

Now $color will be defined with the color that was choosen.

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.