Page 1 of 1

echo code which contains a function call

Posted: Tue Feb 26, 2008 1:28 pm
by ianhull
Hi all, can anyone please tell me if it is possible to call a function from code that i am echo'ing out?

here is what i am echo'ing.

Code: Select all

<form action="contact-send/" method="post" enctype="multipart/form-data" name="contactForm" target="_top">
<table width="100%" border="0" cellspacing="3" cellpadding="3">
  <tr>
    <td colspan="4">Please complete the form below to contact us, a company representative will contact you in return shortly.</td>
    </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td align="right">Title:</td>
    <td><span id="spryselect1">
      <select name="title" id="title">
        <option value="Please Select">Please Select</option>
        <option value="Mr">Mr</option>
        <option value="Mrs">Mrs</option>
        <option value="Miss">Miss</option>
        <option value="Ms">Ms</option>
        <option value="Dr">Dr</option>
        <option value="Sir">Sir</option>
        <option value="Jr">Jr</option>
            </select>
      <span class="selectInvalidMsg">Required.</span>      <span class="selectRequiredMsg">Please select an item.</span></span></td>
    <td align="right">Firstname:</td>
    <td><span id="sprytextfield1">
      <input type="text" name="firstname" id="firstname">
      <span class="textfieldRequiredMsg">Required.</span></span></td>
  </tr>
  <tr>
    <td align="right">Surname:</td>
    <td><span id="sprytextfield2">
      <input type="text" name="surname" id="surname">
      <span class="textfieldRequiredMsg">Required.</span></span></td>
    <td align="right">Email:</td>
    <td><span id="sprytextfield3">
    <input type="text" name="email" id="email">
    <span class="textfieldRequiredMsg">Required.</span><span class="textfieldInvalidFormatMsg">Invalid Email.</span></span></td>
  </tr>
  <tr>
    <td align="right">Subject:</td>
    <td><span id="spryselect2">
      <select name="subject" id="subject">
        <option value="Please Select">Please Select</option>
        '.optionSubjects();.'
            </select>
      <span class="selectInvalidMsg">Required.</span>      <span class="selectRequiredMsg">Please select an item.</span></span></td>
    <td align="right">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td align="right" valign="top">Message:</td>
    <td colspan="3"><span id="sprytextarea1">
      <textarea name="message" id="message" cols="45" rows="5"></textarea>
      <span class="textareaRequiredMsg">Required.</span></span></td>
    </tr>
  <tr>
    <td align="right">&nbsp;</td>
    <td><input type="submit" name="button" id="button" value="Send"></td>
    <td align="right">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
 
</form>
notice the function "optionSubjects()"

thanks in advance.

Re: echo code which contains a function call

Posted: Tue Feb 26, 2008 1:35 pm
by JAM
Remove the ; from the call... And add echo's..

Code: Select all

<?php
    function foo() {
        return 'bar';
    }
    echo 'Something '.foo().' here!';
 
    // or...
 
    function foo_two() {
        echo 'bar';
    }
?>
Something <?php foo_two(); ?> here!
Edit; Better examples... Hopefully... ;)

Re: echo code which contains a function call

Posted: Tue Feb 26, 2008 1:37 pm
by Zoxive
I think he means something along the lines of..

Code: Select all

<!-- ..... -->
<option value="Please Select">Please Select</option>
<?php optionSubjects(); ?>
</select>
<!-- ..... -->

Re: echo code which contains a function call

Posted: Tue Feb 26, 2008 1:46 pm
by ianhull
Thanks guys, however, the form i am echo'ing is comming from the db.

i have a function called drawForm($formname);

this then draws the form, in the form i would like to call another function while it is being echo'd which is optionSubjects(); as i would like to populate the subjects list with the options from the db too.

here is my functions

Code: Select all

 
 
 
function drawForm($formName){
 
connectMySQL('STS');
 
$getForm = mysql_query("SELECT * FROM system_forms WHERE formName = '$formName' LIMIT 1")or die(mysql_error());
 
if(mysql_affected_rows() == 0){
echo 'Internal Error! (function drawForm('.$formname.')';
}else{
while($formRecieved = mysql_fetch_array($getForm)){
extract($formRecieved);
echo ''.$formContents.'';
};//end while
};//end if
 
};//end function
 
formContents now holds this data

Code: Select all

 
 
<form action="contact-send/" method="post" enctype="multipart/form-data" name="contactForm" target="_top">
<table width="100%" border="0" cellspacing="3" cellpadding="3">
  <tr>
    <td colspan="4">Please complete the form below to contact us, a company representative will contact you in return shortly.</td>
    </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td align="right">Title:</td>
    <td><span id="spryselect1">
      <select name="title" id="title">
        <option value="Please Select">Please Select</option>
        <option value="Mr">Mr</option>
        <option value="Mrs">Mrs</option>
        <option value="Miss">Miss</option>
        <option value="Ms">Ms</option>
        <option value="Dr">Dr</option>
        <option value="Sir">Sir</option>
        <option value="Jr">Jr</option>
            </select>
      <span class="selectInvalidMsg">Required.</span>      <span class="selectRequiredMsg">Please select an item.</span></span></td>
    <td align="right">Firstname:</td>
    <td><span id="sprytextfield1">
      <input type="text" name="firstname" id="firstname">
      <span class="textfieldRequiredMsg">Required.</span></span></td>
  </tr>
  <tr>
    <td align="right">Surname:</td>
    <td><span id="sprytextfield2">
      <input type="text" name="surname" id="surname">
      <span class="textfieldRequiredMsg">Required.</span></span></td>
    <td align="right">Email:</td>
    <td><span id="sprytextfield3">
    <input type="text" name="email" id="email">
    <span class="textfieldRequiredMsg">Required.</span><span class="textfieldInvalidFormatMsg">Invalid Email.</span></span></td>
  </tr>
  <tr>
    <td align="right">Subject:</td>
    <td><span id="spryselect2">
      <select name="subject" id="subject">
        <option value="Please Select">Please Select</option>
 
 
        <?php optionSubjects(); ?> //this is being echo'd, i would like to populate the options from this function
 
 
            </select>
      <span class="selectInvalidMsg">Required.</span>      <span class="selectRequiredMsg">Please select an item.</span></span></td>
    <td align="right">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td align="right" valign="top">Message:</td>
    <td colspan="3"><span id="sprytextarea1">
      <textarea name="message" id="message" cols="45" rows="5"></textarea>
      <span class="textareaRequiredMsg">Required.</span></span></td>
    </tr>
  <tr>
    <td align="right">&nbsp;</td>
    <td><input type="submit" name="button" id="button" value="Send"></td>
    <td align="right">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
 
</form>
 
 
 
any ideas greatly appreciated

Re: echo code which contains a function call

Posted: Tue Feb 26, 2008 1:52 pm
by Zoxive
Is the text "<?php optionSubjects(); ?>" literally being output?

Does that page(With all the html) end in .php, so the server knows how to interpret it?

Re: echo code which contains a function call

Posted: Tue Feb 26, 2008 1:53 pm
by ianhull
yes it does.

a simple index.php till i get started :P

thats right, all i get is <option value="Please Select">Please Select</option>
<?php optionSubjects(); ?>
</select>

Re: echo code which contains a function call

Posted: Tue Feb 26, 2008 2:03 pm
by Zoxive
I see what you are doing now. All the text is being pulled from the database as a string, so all php knows is that it is a string.

The easiest way is to str_replace the text with the actual data.

Example..

Code: Select all

// Make sure this function returns the html, and does not echo it.
$Data = optionSubjects();
// Text From Database
$TextFromDB = str_replace('{PlaceHolder}',$Data,$TextFromDB);
 

Re: echo code which contains a function call

Posted: Tue Feb 26, 2008 4:05 pm
by ianhull
Thanks so much zoxive,

However, i am only getting one result returned.

any ideas on how to return the array of values?

here is my function

Code: Select all

 
 
function optionSubjects(){
 
connectMySQL('STS');
 
$getSubjects = mysql_query("SELECT * FROM _option_subjects ORDER BY label ASC")or die(mysql_error());
 
while($subjectsRecieved = mysql_fetch_array($getSubjects)){
extract($subjectsRecieved);
return '<option value="'.$label.'">'.$label.'</option>';
};//end while
};//wnd function
 
 

Code: Select all

function drawForm($formName){
 
connectMySQL('STS');
 
$getForm = mysql_query("SELECT * FROM system_forms WHERE formName = '$formName' LIMIT 1")or die(mysql_error());
 
if(mysql_affected_rows() == 0){
echo 'Internal Error! (function drawForm('.$formname.')';
}else{
$subjects = optionSubjects();
while($formRecieved = mysql_fetch_array($getForm)){
extract($formRecieved);
$formContents = str_replace('{SUBJECTS}',$subjects,$formContents);
 
echo ''.$formContents.'';
};//end while
};//end if
 
};//end function

Re: echo code which contains a function call

Posted: Tue Feb 26, 2008 4:16 pm
by ianhull
I managed it, thanks again for your help.

Code: Select all

 
 
 
function optionSubjects(){
 
connectMySQL('STS');
 
$getSubjects = mysql_query("SELECT * FROM _option_subjects ORDER BY label ASC")or die(mysql_error());
 
while($subjectsRecieved = mysql_fetch_array($getSubjects)){
extract($subjectsRecieved);
$options .= '<option value="'.$label.'">'.$label.'</option>';
};//end while
return $options;
};//end function