Echo block of HTML that contains PHP functions - php

I have a simple if-else statement--if true, I'd like it to echo this content:
<div id="login">
<h2>Login</h2>
<div class="box">
<form method="POST" action="index.php/login">
Username/Email:<br />
<input type="text" name="username" value="<?php echo set_value("username"); ?>" size="50" class="form" />
<?php echo form_error("username"); ?>
Password:<br />
<input type="password" name="password" value="<?php echo set_value("password"); ?>" size="50" class="form" />
<?php echo form_error("password"); ?>
<input type="submit" value="Login" name="login" />
</form>
</div>
</div>
and if false do something similar.
How should I approach this? I'm a PHP noob (and I've been annoying the community here!)--so I'm standing at the echo function right now--I think I can set variables, but that's about it.
Thanks so much!

Same thing as you have been doing:
<?php
if some_condition
{
?>
<p> I am inside the true condition due : <?php echo "true on condition" ?> </p>
<?
}
else
{
?>
<p> I am inside the false condition due : <?php echo "false on condition" ?> &lt/p>
<?
}
?>

You can simply break out of PHP in the if/else statement.
if(true)
{
?>
Regular HTML Here
<?php
}
else
{
?>
Regular HTML Here
<?php
}
You can break out of PHP at any time using the '?>' operator, and with any control structure, it should show the code just as if you echo'd it. Another option would be to use output buffering.
if(true)
{
ob_start();
?>
Regular Code Here
<?php
$contents = ob_get_contents();
ob_end_clean();
}
This will leave the contents of what outputted between the start and end in $contents.
Finally, you could use an include() to only include your login form when you actually need it. (I prefer this method because I can put login forms almost anywhere on the site very easily)
if(true)
{
include('loginform.php');
}

<?php if ($condition) { ?>
<div id="login">
<h2>Login</h2>
<div class="box">
<form method="POST" action="index.php/login">
Username/Email:<br />
<input type="text" name="username" value="<?php echo set_value("username"); ?>" size="50" class="form" />
<?php echo form_error("username"); ?>
Password:<br />
<input type="password" name="password" value="<?php echo set_value("password"); ?>" size="50" class="form" />
<?php echo form_error("password"); ?>
<input type="submit" value="Login" name="login" />
</form>
</div>
</div>
<?php } else { ?>
other html here
<?php } ?>

Just a suggestion; for readability and ease of flexibility, it is better to place what you want to display inside a variable first.
$html = "<form method='post' action='?'> ...... ";
if ($condition)
$html .= "Error"
else
$html .= "No problem"
....
<html>
<body>
<?php
echo $html;
?>
</body>
</html>
Or you can use a template. One which I would recommend is EasyTemplate.

there's a form of php conditional that's somewhat easier to read for this, uses colon notation so instead of
<?php if ($condition) { ?>
...html
<?php } else { ?>
...html
<?php } endif; ?>
you do
<?php if ($condition) : ?>
...html
<?php else : ?>
...html
<?php endif; ?>
also if your config allows it, you can shorthand
<?= $variable ?>
instead of
<?php echo($variable); ?>

Dropping out of php is the easiest way to do this. I find that using the alternate control structures makes this sort of code much more readable than stranded curly braces, especially if you have a lot of html.
<?php if (!$logged_in): ?>
<div id="login">
<h2>Login</h2>
<div class="box">
<form method="POST" action="index.php/login">
Username/Email:<br />
<input type="text" name="username" value="<?php echo set_value("username"); ?>" size="50" class="form" />
<?php echo form_error("username"); ?>
Password:<br />
<input type="password" name="password" value="<?php echo set_value("password"); ?>" size="50" class="form" />
<?php echo form_error("password"); ?>
<input type="submit" value="Login" name="login" />
</form>
</div>
</div>
<?php else: ?>
// display html for a logged-in user
<?php endif; ?>

Use something like this
echo htmlentities($string);
htmlentities
Set the string to the html code you want to output.

Related

php how include and run method from php file where is html code?

I wrote my index.php file, its this:
<?php // index.php
if(isset($_SESSION['login_user'])) {
header("location: profile.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Bejelentkezés</title>
</head>
<body>
<div>
<div>
<h1>Wellis adatok</h1>
<div>
<h2>Bejelentkezés</h2>
<form action="login.php" method="post">
<label>Felhasználó neve :</label>
<input id="name" name="userName" placeholder="név" type="text"></br></br>
<label>Jelszó :</label>
<input id="password" name="password" placeholder="**********" type="password" style="width: 80px"></br></br>
<input name="submit" type="submit" value=" Login ">
<span><?php echo $error; ?></span>
</form>
</div>
</div>
</div>
<p>
</br>
Listák megtekintése:
<?php
include("functions.php");
welcome();
?>
</p>
</body>
</html>
Also there is an other: functions.php, where are lots of functions. One of those is this:
<?php // functions.php
function welcome() {
echo "Welcome here ...";
}
...
?>
I wanna run that welcome() method there. This is only sample but orhers are same like this. I wanna see welcome message below login fields and submit button.
The page is hungarian but the page works well except this probleme.
Thanks for quick help ...

WP PHP Form Fields are missing. Where are they?

This is a hopeful question. I am really, really PHP inexperienced. But I am trying to teach myself a thing or two. The organization I work for uses a WP site. I regularly work on it. I mostly do HTML and CSS (stuff I know about) but I was just digging around in the background and came across something I don't understand at all.
I made a test page here: http://all4ed.org/webinar-event/test-2/
There are two forms on the page. The first ends in a "submit button," the second ends in a "Register Button."
So far so good. I dug around the backend of the WP files, and found the content-webinar.php file and saw that it pulled the form in as an include with:
// The registration form. ?>
<?php if ($registration_required == 'yes'): ?>
<?php include ('assets/includes/webinar/inc-block-registration-form.php'); ?>
<?php endif;
Good, so far.
I went to the "inc-block-registration-form.php" file and here's where it gets weird:
The first form is done with this code:
<form class="webinar-registration" action="<?= $_SERVER["REQUEST_URI"] ?>" method="get">
<div>
<p>
<label for="token">Token</label>
<input type="text" name="token" />
</p>
<p>
<button>Submit</button>
</p>
</div>
</form>
This looks exactly like I thought it would. The second form is written like this:
<form class="webinar-registration" action="<?= $_SERVER["REQUEST_URI"] ?>" method="post">
<div>
<?php if ($webinars->error) : ?>
<p class="error"><?= $webinars->error ?></p>
<?php endif; ?>
<?php foreach ($webinars->form_data as $data) : ?>
<p><?= $webinars->build($data) ?></p>
<?php endforeach; ?>
<p>
<button>Register</button>
<input type="hidden" name="webinars_registration_submitted" value="<?= $post->ID ?>" />
<input type="hidden" name="webinars_registration_title" value="<?= get_the_title() ?>" />
<input type="hidden" name="webinars_registration_url" value="<?= get_permalink() ?>" />
<!-- fields necessary for AA -->
<input type="hidden" name="webinars_user_id" value="<?= get_current_user_id() ?>" />
<input type="hidden" name="webinars_type" value="<?= get_field('webinar_type') ?>" />
<input type="hidden" name="webinars_post_id" value="<?= get_the_ID () ?>" />
</p>
</div>
</form>
Huh???? Where is the "Your Name", "Your E-mmail Address," "Organization," "City, State," or "Question (This is optional)"
How does this work? Where is it pulling those fields from. Any insight would be appreciated. Thanks.

Passing hidden value to a php variable in input tag

I am new to coding and pardon me if it a silly thing I am missing. I have searched through the forum & did not find an answer that suits my need. I have 2 files: jobs.php & jobprocess.php
Jobs.php goes as
<?php session_start();
include('dbConnect.php');
$q1="abc";
$q2="pqr";
$q3="xyz";
$opportunity=29;
echo "Opportunity is". $opportunity;
?>
<html>
<head>
<div align="center">
<form method="post" method="post" action="jobprocess.php">
<input type="text" name="q1" placeholder="<?php echo $q1;?>"><br>
<input type="text" name="q2" placeholder="<?php echo $q2;?>"><br>
<input type="text" name="q3" placeholder="<?php echo $q3;?>"><br>
<input type="hidden" name="opportunity" value="<?php echo $opportunity;?>">
<ul class="actions">
<li><input type="submit" name="submit" value="I would like to join!! "></li>
</ul>
</form>
</div>
</head>
<body>
</body>
</html>
jobprocess.php goes with the code
<?php session_start();
include('dbConnect.php');
$opportunity = $_GET['opportunity'];
echo "opportunity is " . $opportunity;
?>
Unfortunately, the above code is not defining value="29" for opportunity on 2nd page. Thanks in advance
If you echo anything before the html tag it would effectively make the html invalid. Also, the head of the document MUST not have presentational html elements such as forms, divs etc
<?php
session_start();
include('dbConnect.php');
$q1="abc";
$q2="pqr";
$q3="xyz";
$opportunity=29;
?>
<html>
<head>
<title>must have a title</title>
</head>
<body>
<?php
echo "Opportunity is". $opportunity;
?>
<div align="center">
<form method="post" method="post" action="jobprocess.php">
<input type="text" name="q1" placeholder="<?php echo $q1;?>"><br>
<input type="text" name="q2" placeholder="<?php echo $q2;?>"><br>
<input type="text" name="q3" placeholder="<?php echo $q3;?>"><br>
<input type="hidden" name="opportunity" value="<?php echo $opportunity;?>">
<ul class="actions">
<li><input type="submit" name="submit" value="I would like to join!! "></li>
</ul>
</form>
</div>
</body>
</html>
And because the form is set to POST you should probably check and use the POSTed variable rather than a GET variable
<?php
session_start();
include('dbConnect.php');
$opportunity = $_POST['opportunity'];
echo "opportunity is " . $opportunity;
?>
Surprisingly my answer that suggested using session variable instead of hidden form field was deleted?! I guess session variables are illegal now?
The answer was chosen for the best answer even.

HTML & PHP - How to make multiple forms submittable

I have the following code which loops through an array to create forms that are filled with values (which the user will be able to edit) on a single page. There are as many forms as there are loops in the array, which is always changing.
<body>
<div id="main">
<?php
foreach($articles as $item) { ?>
<div id='container'>
<form>
Title: <input type="text" name="title" size="80" value="<?php echo $item[0]; ?>">
<br>
URL: <input type="text" name="url" size="80" value="<?php echo $item[1]; ?>">
<br>
End Date: <input type="text" name="endDate" value="<?php echo substr($item[7], 14, strpos($item[7], '#') - strlen($item[7])); ?>">
<br>
<?php
if (substr($item[8], 0, 2) === 'Su'){
} else {
?>
Start Date: <input type="text" name="startDate" value="<?php echo substr($item[8], 7, 9); ?>">
<?php } ?>
</form>
</div>
<?php } ?>
</div>
</body>
Now, I want the user to have a single submit button at the bottom of the page which will submit ALL the forms on the page to MySQL database. The problem is I don't know how to do that.
I know the submit button takes the format of
<input type="submit" value="Submit">
I am assuming I need to give each form in the loop a unique name but from there I am at a loss as to what my next step should be to actually send and receive the information from these multiple forms.
Any help would be appreciated. Thanks.
You can't submit more than one form at once. What is wrong with putting all sets of <input>s into a single form?:
<body>
<div id="main">
<form>
<?php
$inpCnt = 0;
foreach($articles as $item) {
$inpCnt++; ?>
<div id='container'>
Title: <input type="text" name="title_<?php echo $inpCnt; ?>" size="80" value="<?php echo $item[0]; ?>">
<br>
URL: <input type="text" name="url_<?php echo $inpCnt; ?>" size="80" value="<?php echo $item[1]; ?>">
<br>
End Date: <input type="text" name="endDate_<?php echo $inpCnt; ?>" value="<?php echo substr($item[7], 14, strpos($item[7], '#') - strlen($item[7])); ?>">
<br>
<?php
if (substr($item[8], 0, 2) === 'Su'){
} else {
?>
Start Date: <input type="text" name="startDate_<?php echo $inpCnt; ?>" value="<?php echo substr($item[8], 7, 9); ?>">
<?php } ?>
</div>
<?php } ?>
</form>
</div>
</body>
You need to be able to define each of these inputs aswel. So I've used the loop to give each one a unique name.

can't i display two session message in one page

i have been displaying two session messages in one page, rating. One message is displayed in every refresh showing the likes and dislikes. the other message in displayed once the rating form is submitted, the problem is once i submit the rating form the message displaying the rating isn't displayed. why is so??? can't i use two sessions in a page?? thanks
coding
<?php
if(!empty($_SESSION['error_msg'])){?>
<div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<?
session_unset($_SESSION['error_msg']);
}
?>
if(!empty($_SESSION['rate_msg'])){
echo $_SESSION['rate_msg'];
session_unset($_SESSION['rate_msg']);
}
?>
<form name="rating_form" action="" method="post">
<input type="hidden" name="g_id" value="<?php echo $ratings['gallery_id'];?>"/>
<input type="hidden" name="g_image" value="<?php echo $ratings['gallery_image'];?>"/>
<div style="text-align: center">
<input type="radio" name="rating" value="Hot" onclick="document.rating_form.submit();">Hot</input>
<input type="radio" name="rating" value="Not" onclick="document.rating_form.submit();">Not</input>
</div>
</form>
sorry i think my code is a bit confusing
my actual code looks like this
<div class="top_model">
<h1>Model Ratings</h1>
<?php
if(!empty($_SESSION['error_msg'])){?>
<div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<?
session_unset($_SESSION['error_msg']);
}
?>
<ul class="homemodel_thumbs">
<div style="text-align: right">
</div>
<?php
if(!empty($ratingArr)){
foreach($ratingArr as $ratings){
?>
<li><img src="<?php echo base_url();?>images/gallery/large/<?php echo $ratings['gallery_image'];?>" alt="<?php echo $ratings['gallery_image'];?>" class="center_image"/>
<p> </p>
<?php
if(!empty($_SESSION['rate_msg'])){
echo $_SESSION['rate_msg'];
//session_unset($_SESSION['rate_msg']);
}
?>
<form name="rating_form" action="" method="post">
<input type="hidden" name="g_id" value="<?php echo $ratings['gallery_id'];?>"/>
<input type="hidden" name="g_image" value="<?php echo $ratings['gallery_image'];?>"/>
<div style="text-align: center">
<input type="radio" name="rating" value="Hot" onclick="document.rating_form.submit();">Hot</input>
<input type="radio" name="rating" value="Not" onclick="document.rating_form.submit();">Not</input>
</div>
</form>
</li>
<?php
}
}
?>
</ul>
this problem is solved. of the two session message for the one i used php session while for the another one i used code igniter session. sorry if i haven't mentioned i am using ci. anyways thanks everyone for the help :)
You're unsetting your rating message after you display it:
session_unset($_SESSION['rate_msg']);
This means it's not available after you've posted back the form unless you re-populate it again.
Remove this and i suspect it'll work.
You have an excess ?> :
<?php
if(!empty($_SESSION['error_msg'])){?>
<div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<?
session_unset($_SESSION['error_msg']);
}
if(!empty($_SESSION['rate_msg']))
{
echo $_SESSION['rate_msg'];
session_unset($_SESSION['rate_msg']);
}
?>
You don't open php tag for last statement, (or unnesesery close it) it's treated as html
<?php
if(!empty($_SESSION['error_msg'])){?>
<div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<?php
session_unset($_SESSION['error_msg']);
}
?>
if(!empty($_SESSION['rate_msg'])){
echo $_SESSION['rate_msg'];
session_unset($_SESSION['rate_msg']);
}
?>
EDIT
<?php
// < HERE IS PHP >
if(!empty($_SESSION['error_msg'])){?>
<div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<!-- HERE IS HTML -->
<?
// < HERE IS PHP >
session_unset($_SESSION['error_msg']);
}
?> <!-- CLOSED PHP - NOW IT'S HTML - YOU SHOULD REMOVE IT -->
<!-- THIS IS NOT EXECUTED - BECAUSE IT'S HTML NOW -->
if(!empty($_SESSION['rate_msg'])){
echo $_SESSION['rate_msg'];
session_unset($_SESSION['rate_msg']);
}
?> <!-- THIS IS NOT PHP TAG BECASUE THERE IS NOT OPEN TAG -->
<form name="rating_form" action="" method="post">
<input type="hidden" name="g_id" value="<?php echo $ratings['gallery_id'];?>"/>
<input type="hidden" name="g_image" value="<?php echo $ratings['gallery_image'];?>"/>
<div style="text-align: center">
<input type="radio" name="rating" value="Hot" onclick="document.rating_form.submit();">Hot</input>
<input type="radio" name="rating" value="Not" onclick="document.rating_form.submit();">Not</input>
</div>
</form>

Categories