How do I echo <?php .....

This is a discussion on "How do I echo <?php ....." within the PHP Forum section. This forum, and the thread "How do I echo <?php ..... are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Sep 14th, 2007, 01:30
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
How do I echo <?php .....

An easy question.....

How do i echo or print this (it's part of an html form) in php

Code: Select all
value="<?php error_field($error, "first_name"); ?>"
Do i use

PHP: Select all

print("value=\"<?php error_field($error"first_name\""); ?>\");
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Sep 14th, 2007, 01:49
Elite Veteran
Join Date: Jan 2007
Location: You know where
Age: 31
Posts: 4,617
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How do I echo <?php .....

The first one ...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Sep 14th, 2007, 01:51
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How do I echo <?php .....

Oops...i forgot to mention that it's in part of a function

PHP: Select all

function error_field($error$field) { 
     if(
$error[$field]) { 
  print(
"value=\"old value\""); 
     } 
     else {
  if(
$print_again) {
      print(
"value=\"new value\"");
  } else {
      print(
"value=\"old value\"");
  }
     }
 } 
I want to replace value=\"old value\" with what I mentioned in first post
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Sep 14th, 2007, 01:58
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How do I echo <?php .....

Actually, i just need to echo a string

PHP: Select all

print("value=\"<?php echo $value?>\"");
But when the form is displayed, in the form text box, I get <?php echo ; ?>

For some reason it's not echoing the variable
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Sep 14th, 2007, 02:00
Elite Veteran
Join Date: Jan 2007
Location: You know where
Age: 31
Posts: 4,617
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How do I echo <?php .....

with echo I usually do

PHP: Select all

echo 'value="'$value .'"'
Can you do something like that with print? I rarely use print so I don't know
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Sep 14th, 2007, 06:24
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How do I echo <?php .....

Is this what you want?:
PHP: Select all

 <?php
function error_field($error$field) { 
  if(
$error[$field]) { 
    echo(
'value='.$value); 
  } else {
    if(
$print_again) {
      echo(
'value='.$value);
    } else {
      echo(
'value='.$value);
    }
  }
 }
 
?>
FYI: 'echo' has better performance than 'print'.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Sep 14th, 2007, 11:18
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How do I echo <?php .....

By using your code I made some changes and now when i view source i can see value="" in the form which obviuosly the variables are getting passed. It should have either "old old value" or "new new value" in the value field.

Here is the function
PHP: Select all

$oldvalue"old old value";
$newvalue"new new alue";
 
 function 
error_field($error$field) { 
     if(
$error[$field]) { 
         echo 
$oldvalue
     }
     else 
     {
                if(
$print_again) {
             echo 
$newvalue;
      } else {
            echo 
$oldvalue;
      }
     }
  } 
and again here is the whole script whcih has some changes to make the function work (look at the form value field)
PHP: Select all

<?php
    
if (!is_authed()) 
    {
 print (
'You need to login to view this page, <a href="index.php?page=login">click here</a> to login.');
    }
    else
    {
 echo 
"<h2>Edit Profile - ".$_SESSION['username']."</h2><hr />";
 if(isset(
$_POST['submit']))
 {
             
$submit $_POST['submit'];
 }
 
$result mysql_query("SELECT `first_name`, `last_name`, `username`, `interests` FROM `users` WHERE `username`='".$_SESSION['username']."'");
 while(
$row mysql_fetch_assoc($result))
 {
     
$get_first_name $row['first_name'];
     
$get_last_name $row['last_name'];
     
$get_username $row['username'];
     
$get_interests $row['interests'];
 }
$oldvalue"old old value";
$newvalue"new new alue";
 function 
error_field($error$field) { 
     if(
$error[$field]) { 
         echo 
$oldvalue
     }
     else 
     {
                if(
$print_again) {
             echo 
$newvalue;
      } else {
            echo 
$oldvalue;
      }
     }
  }
 function 
check_form() { 
     global 
$_POST$error$print_again;
     global 
$first_name$last_name$username$newsletter
     
$first_name check_input($_POST['first_name']);
     
$last_name check_input($_POST['last_name']);
     
$interests check_input($_POST['interests']);
     
$username check_input($_POST['username']);
     
$newsletter $_POST['newsletter'];
 
     
$error['first_name'] = false;
 
     if(empty(
$first_name)) { 
         
$error['first_name'] = true
          
$print_again true;
         
$message="<li>The <span><b>Forename</b></span> field is empty</li>"
     }
     else if(!
ereg("^[A-Za-z]{2,30}$",$first_name)) {
  
$error['first_name'] = true
          
$print_again true;
  
$old_value $first_name;
         
$message="<li><span><b>Forename</b></span> must contain letters only</li>";
     }
 
     if(empty(
$last_name)) { 
         
$error['last_name'] = true
          
$print_again true
         
$message.="<li>The <span><b>Surname</b></span> field is empty</li>"
     }
     else if(!
ereg("^[A-Za-z\-]{2,30}$",$last_name)) {
  
$error['last_name'] = true
          
$print_again true
         
$message.="<li><span><b>Surname</b></span> must contain letters only</li>";
     }
     if(empty(
$username)) { 
         
$error['username'] = true
          
$print_again true
         
$message.="<li>The <span><b>Username</b></span> field is empty</li>"
     }
     else if( 
mysql_num_rows(mysql_query("SELECT username FROM users WHERE username = '$username'")) ) {
         
$error['username'] = true
          
$print_again true
         
$message.="<li>The username you have selected has already been used by another member in our database. Please choose a different Username!</li>";
     }
     else if(!
ereg("^[A-Za-z0-9 \-]{4,30}$",$username)) {
  
$error['username'] = true
          
$print_again true
         
$message.="<li><span><b>Username</b></span> must contain letters and numbers only</li>";
     }  
     if(
$print_again)
     {
     echo 
"<h2 class=\"errorhead\">There has been an error:</h2><p>You forgot to enter the following field(s)</p>
         <ul id=\"validation\">$message</ul>"
;
     
show_form(); 
     }
     else
     {
  
// Stop the form being used from an external URL
      // Get the referring URL
      
$referer $_SERVER['HTTP_REFERER'];
      
// Get the URL of this page
      
$this_url "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
      
// If the referring URL and the URL of this page don't match then
      // display a message and don't send the email.
      
if ($referer != $this_url)
   {
             echo 
"You do not have permission to use this script from another URL.<br />";
      echo 
"If you are behind a firewall please check your referrer settings.";
             exit;
         }
  
//These just test to make sure what variables are getting passed
  
echo $username."<br>";
  echo 
$first_name."<br>";
  echo 
$last_name."<br>";
  echo 
$interests."<br>";
  echo 
$oldvalue."<br>";
  echo 
$newvalue."<br>";
  echo 
$get_first_name."<br>";
  echo 
$get_username."<br>";
  echo 
$gewt_interests."<br>";
     } 
 }
 function 
show_form() { 
     global 
$_POST$print_again$error;
     global 
$first_name$last_name$interests$username$newsletter
     
?> 
     <form method="post" id="login" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
          <fieldset>
          <legend>Username</legend>
      <p class="hint">Please enter a username for your user account. Note that username should be between 4 and 30 characters.</p>
          <p><label for="username">Username:</label>
          <input type="text" <?php error_bool($error"username"); ?> title="Please enter a username" id="username" name="username" size="30" value="<?php echo $username?>" /></p>
          </fieldset>
          <fieldset>
      <legend>About You</legend>
          <p><label for="first_name">Forename:</label>
          <input type="text" <?php error_bool($error"first_name"); ?> title="Please enter your first name" id="first_name" name="first_name" size="30" value="<?php error_field($error"first_name"); ?>" /></p>
          <p><label for="last_name">Surname:</label>
          <input type="text" <?php error_bool($error"last_name"); ?> title="Please enter your last name" id="last_name" name="last_name" size="30" value="<?php echo $last_name?>" /></p>
      <p class="hint">Please enter any interests/hobbies you have (optional).</p>
          <p><label for="interests">Your Interests:</label>
          <input type="text" title="Please enter any interests/hobbies you have" id="interests" name="interests" size="30" value="<?php echo $interests?>" /></p>
          </fieldset>
          <fieldset>
          <legend>Newsletter</legend>
      <p class="hint">Sign up for jackgodfrey.org.uk newsletter. This newsletter includes what forthcoming events we are arranging, the latest Honeylands news and our latest news. You can Opt-out at any time....so sign up and give it a try!!</p>
          <p><label for="newsletter">Newsletter:</label>
          <input style="border:none" type="radio" value="yes" id="newsletter" checked="checked" name="newsletter" />Opt-in <input style="border:none" type="radio" value="no" name="newsletter" />Opt-out</p>
          </fieldset>
          <p><label for="Submit" style="width: 20px">&nbsp;</label>
          <input type="submit" name="submit" value="Edit Profile" class="sendbutton" />
          <input type="reset" value="Reset Fields" class="sendbutton" /></p>
     </form>
     <?php
 

    if(isset(
$submit)) { 
     
check_form(); 
    } else { 
     
show_form(); 
    }
}
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Sep 14th, 2007, 11:34
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How do I echo <?php .....

You need to delclare $oldvalue and $newvalue as global in the error_field function as they currently cannot be found in this function's scope.

You are probably getting some undeclared variables warnings in your error log.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Sep 14th, 2007, 11:43
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How do I echo <?php .....

Many thanks Rakuli, you've helped me out yet again

I did think that i needed a global variable but i didn't know where to put it. i did try the check_form and show_form functions but it didn't work
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Sep 14th, 2007, 12:42
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How do I echo <?php .....

Now that I have it doing what I want, I need to pass variables between functions

What I do is connect to the database, assign some variables with values from the database and I need to assign $oldvalue in the validation check to each of the variable taken from the database.

Variables taken from database
PHP: Select all

$get_first_name $row['first_name'];
$get_last_name $row['last_name'];
$get_username $row['username'];
$get_interests $row['interests']; 
This is one part of the validation where i try to assign $oldvalue
PHP: Select all

else if(!ereg("^[A-Za-z]{2,30}$",$first_name)) {
  
$error['first_name'] = true;
          
$print_again true;
  
$old_value $get_first_name;
         
$message="<li><span><b>Forename</b></span> must contain letters only</li>";
     } 
and i need to pass it to here
PHP: Select all

function error_field($error$field) {
 global 
$oldvalue
     if(
$error[$field]) { 
         echo 
$oldvalue
     }
     else 
     {
                if(!
$print_again) {
             echo 
$oldvalue;
      }
     }
  } 
and i thought I had better do this
PHP: Select all

function check_form() { 
     global 
$_POST$error$print_again$oldvalue$get_first_name
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Sep 14th, 2007, 13:07
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How do I echo <?php .....

Silly observation but your global declaration uses $oldvalue and the assignment uses $old_value (note the underscore -- this might explain why it isn't populating?

Also notice you're using a regex to check for just letters, I would suggest using ctype_aplha($first_name) which will return false if anything apart from an alphabet character is found.

Cheers
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Sep 14th, 2007, 13:16
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How do I echo <?php .....

I checked what was getting echoed at the end and all the variables are getting echoed so those variables are getting passed through the check_form() function.

I did some further tests and I found that $old value was not getting assigned anything

The problem lies in this bit I believe
PHP: Select all

     else if(!ereg("^[A-Za-z]{2,30}$",$first_name)) {
  
$error['first_name'] = true;
          
$print_again true;
   
$old_value $get_first_name//This bit is causing the error
         
$message="<li><span><b>Forename</b></span> must contain letters only</li>";
     }