PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
ianhull
Forum Contributor
Posts: 310 Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK
Post
by ianhull » Tue Feb 26, 2008 1:28 pm
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> </td>
<td> </td>
<td> </td>
<td> </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"> </td>
<td> </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"> </td>
<td><input type="submit" name="button" id="button" value="Send"></td>
<td align="right"> </td>
<td> </td>
</tr>
</table>
</form>
notice the function "optionSubjects()"
thanks in advance.
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Tue Feb 26, 2008 1:35 pm
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...
Last edited by
JAM on Tue Feb 26, 2008 1:38 pm, edited 2 times in total.
Zoxive
Forum Regular
Posts: 974 Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan
Post
by Zoxive » Tue Feb 26, 2008 1:37 pm
I think he means something along the lines of..
Code: Select all
<!-- ..... -->
<option value="Please Select">Please Select</option>
<?php optionSubjects(); ?>
</select>
<!-- ..... -->
ianhull
Forum Contributor
Posts: 310 Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK
Post
by ianhull » Tue Feb 26, 2008 1:46 pm
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> </td>
<td> </td>
<td> </td>
<td> </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"> </td>
<td> </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"> </td>
<td><input type="submit" name="button" id="button" value="Send"></td>
<td align="right"> </td>
<td> </td>
</tr>
</table>
</form>
any ideas greatly appreciated
Zoxive
Forum Regular
Posts: 974 Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan
Post
by Zoxive » Tue Feb 26, 2008 1:52 pm
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?
ianhull
Forum Contributor
Posts: 310 Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK
Post
by ianhull » Tue Feb 26, 2008 1:53 pm
yes it does.
a simple index.php till i get started
thats right, all i get is <option value="Please Select">Please Select</option>
<?php optionSubjects(); ?>
</select>
Zoxive
Forum Regular
Posts: 974 Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan
Post
by Zoxive » Tue Feb 26, 2008 2:03 pm
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);
ianhull
Forum Contributor
Posts: 310 Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK
Post
by ianhull » Tue Feb 26, 2008 4:05 pm
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
ianhull
Forum Contributor
Posts: 310 Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK
Post
by ianhull » Tue Feb 26, 2008 4:16 pm
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