Page 1 of 1

[Solved] Passing '%' if possible

Posted: Fri Aug 19, 2005 12:18 pm
by Addos
I’m not sure how to go about this. I have a select box with the following:

Code: Select all

<?PHP echo "<select name='property' id='property'>\n
	  <option value=\"'%'\">Select</option>"; 
		while($dbRow = mysql_fetch_array($GetProperty)){ 
  		echo "<option value='"
		. $dbRow["name"]  
		. "'>"
		. $dbRow["name"]
		."</option>\n"; 
		} echo "</select>\n"; 
		?>
Displays:
House 1
House 2
House 3

I’m setting up a conditional statement that ensures that a selection is made so that the word ‘Select’ is not entered into the database rather than a house name from the select list.

I’m trying to do something using this:

Code: Select all

if ($_POST['property'] != "\'%\") {
      $messageproperty = '<b>Required</b>';}

But it is not fully returning what I want. If I use echo $_POST['property'] ; I can see \'%\' being displayed in the browser and if I make a selection this is also returns the selected text ok. If I physically add House 3 into if ($_POST['property'] != " House 3 ") then I get the desired result so I think I’m close but don’t fully understand how to pass this \'%\' .


Code: Select all

echo $_POST['property'] ;
    if (!trim($trimed_fullname) && !empty($_POST['fullname'])) {
      $messagefname = '<b>Required!</b>';
    }

<?php
if (isset($messageproperty) && !empty($messageproperty)) {
  echo $messageproperty; } ?>
Any help is most appreciated
Thanks a mil
B

Posted: Fri Aug 19, 2005 12:57 pm
by korto

Code: Select all

if($_POST['property']!='%') //won't this work?

Posted: Fri Aug 19, 2005 1:09 pm
by Addos
Thanks for your reply.
No this doesn’t work either in that I keep seeing ‘Required’ regardless of whether I make a selection or not.
Thanks again for your help
B

Posted: Fri Aug 19, 2005 1:12 pm
by korto
Sorry, it should be

Code: Select all

if($_POST['property']=='%') {messageproperty='Required';}

Posted: Fri Aug 19, 2005 1:14 pm
by nielsene
Notice that in your option you set the value = to "'%'", so the single quotes surrounding the % are part of the value. Therefore you'll want to test

Code: Select all

if ($_POST['property']=="'%'") ...
Notice the single quotes on both sides of the percent, inside the doubles.

Posted: Fri Aug 19, 2005 1:51 pm
by Addos
Hi Guy’s thanks for your patience with me on this.

Code: Select all

if ($_POST['property'] =="'%'") {
      $messageproperty = '<b>Required</b>'; 

  if ($_POST['property'] =='%') {
      $messageproperty = '<b>Required</b>';
Unfortunately doesn’t return ‘Required’ at all. I wonder (as a beginner I might add) if I can test for \'%\' as in the backslashes too as this is what is echoed in the browser.

Thanks
B
:wink:

Posted: Fri Aug 19, 2005 2:45 pm
by wwwapu
The problem might lie here:

Code: Select all

<?PHP echo "<select name='property' id='property'>\n 
      <option value=\"'%'\">Select</option>";
which prints

Code: Select all

<option value="'%'">Select</option>
if you really want to pass '%' by post check it with

Code: Select all

if ($_POST['property'] =='\'%\'') { 
      $messageproperty = '<b>Required</b>';
Or if you wish to pass % by post print it with

Code: Select all

<?PHP echo "<select name='property' id='property'>\n 
      <option value=\"%\">Select</option>";

Posted: Fri Aug 19, 2005 3:33 pm
by Addos
Thanks for your help Jari but I still am getting no result. Basically as before the ‘Required’ message is not showing. I have tried everything I can but I’m at a complete loss. I have posted a good bit of the code I’m using but I wonder though that running a conditional statement must be normal enough for select boxes but how it this normally done or what am I doing wrong in my approach?
Thanks again all help is most gratefully received.
B

<snip>

Code: Select all

<?PHP
?> <!--Property Conditional-->
 <?PHP
 
  if ($_POST['property'] =='\'%\'') {
      $messageproperty = '<b>Required</b>';
    }
	echo $_POST['property'] ;
{

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO enquiries (property) VALUES (%s)",
                       GetSQLValueString($_POST['property'], "text"));
                      
<form method="post" name="form1" action="<?php $_SERVER['PHP_SELF']?>">
        <table>
          <tr valign="baseline">
            <td align="right" nowrap id="error">* Property
		<?php
		if (isset($messageproperty) && !empty($messageproperty)) {
		   echo $messageproperty; } ?>
			</td>
            <td>
			<!--Property List-->
		<?PHP echo "<select name='property' id='property'>\n
	  <option value=\"'%'\">Select</option>"; 
		while($dbRow = mysql_fetch_array($GetProperty)){ 
  		echo "<option value='"
		. $dbRow["name"]  
		. "'>"
		. $dbRow["name"]
		."</option>\n"; 
		} echo "</select>\n"; 
		?>
		</td>
          </tr>
          
			<td><input name="sendCom"  type="submit" id="sendCom" value="Continue" /></td>
		    </tr>
        </table>
		<input type="hidden" name="MM_insert" value="form1">
      </form></td>
</tr>
</table>

Posted: Fri Aug 19, 2005 5:30 pm
by korto
Are you sure this is he exact code used? It has some obvious syntax errors, for example in line 9 where does that { comes from ? And in any case I really don't see the point of assigning '%' as the value the first option instead of just %, in other words $value='%' and not $value=\"'%'\"

Posted: Sat Aug 20, 2005 3:35 am
by wwwapu
Here is a working examble with % passed. If I were you I'd use just % it's much less trouble with quotation marks. Also you might want to think again all these <?php blocks, code below is quite hard to read and it's easy to make mistakes.

Code: Select all

<?php
if (isset($_POST['property']) && $_POST['property'] == '%') { 
      $messageproperty = '<b>Required</b>';
}else $messageproperty=$_POST['property'];
?>

<form action="<?php print $_SERVER['PHP_SELF']?>" method="post">
<p>
Property 
<?php if (!empty($messageproperty)) { 
  echo $messageproperty; 
}
?></p>
<?PHP echo "<select name='property' id='property'>\n 
      <option value=\"%\">Select</option>
      <option value=\"some\">some</option> 
      <option value=\"thing\">thing</option>";
        while($dbRow = $dbRow = mysql_fetch_array($GetProperty)){ 
          echo '<option value="'.$dbRow["name"].'">'.$dbRow["name"].'</option>\n';
        }echo "</select>\n";
?>      
<input type="submit">
</form>
And the next passes '%'

Code: Select all

<?php
if (isset($_POST['property']) && $_POST['property'] == '\'%\'') { 
      $messageproperty = '<b>Required</b>';
}else $messageproperty=$_POST['property'];
?>

<form action="<?php print $_SERVER['PHP_SELF']?>" method="post">
<p>
Property 
<?php if (!empty($messageproperty)) { 
  echo $messageproperty; 
}
?></p>
<?PHP echo "<select name='property' id='property'>\n 
      <option value=\"'%'\">Select</option>
      <option value=\"some\">some</option> 
      <option value=\"thing\">thing</option>";
        while($dbRow = $dbRow = mysql_fetch_array($GetProperty)){ 
          echo '<option value="'.$dbRow["name"].'">'.$dbRow["name"].'</option>\n';
        }echo "</select>\n";
?>      
<input type="submit">
</form>

Posted: Sat Aug 20, 2005 12:13 pm
by Addos
Thanks Guy's I finally sorted this out with your help. In the end I mamaged to use two different methods all from this post, so thanks again
B :wink:

Posted: Sat Aug 20, 2005 12:18 pm
by Sander
Can't you just pass a 0 (zero) and then do a "if(!$_POST['option'])" ?