After my form has been submitted and my message has been sent I want to go back to the index which this does, but I also want to display echo 'Message has been sent' in the index.php in a div instead of my process.php, how would I go about doing this. Sort of new to php if you need any more of my code I will provide or link to site. Thanks.
This is what i tried so far.
in my process.php file
if(!$mail->send()) {
$output = 1;
// $output = 'Mailer Error: ' . $mail->ErrorInfo;
} else {
$output = 2;
header('Location:index.php');
}
in my index.php file
<?php
if ($output == 2) {
echo "<b>Message has been sent</b>";
} elseif ($output == 1) {
echo "<b>Message could not be sent, please try again</b>";
} else {}
?>
The variable $output isn't set in your index.php file.
As an easy way to start you can try it like
header('Location:index.php?output='.$output);
and fetch output in your index.php with
$output = $_GET['output'];
at the beginning of your file, that way you will be able to make your if statements work.
Also be advised that you will never be redirected if $output = 1 in your process.php, as the header is only in the else statement. Simply place the header after your else statements closing bracket.
if(!$mail->send()) {
$output = 1;
} else {
$output = 2;
}
header('Location:index.php?output='.$output);
die();
index.php:
<?php
if (isset($_GET['output'])) {
$output = $_GET['output'];
if ($output == 2) {
echo "<b>Message has been sent</b>";
} elseif ($output == 1) {
echo "<b>Message could not be sent, please try again</b>";
}
}
Please be advised that you shouldn't use unsanitized request data (user input and what so ever) in a production environment, as this is a security risk.
This won't work, the value won't get passed to the index this way:
} else {
$output = 2;
header('Location:index.php');
}
Your options (short of redesigning the way it works) are POST or GET;
} else {
$output = 2;
header('Location:index.php?sent=1');
}
Then in your index.php:
<?php
if (isset($_GET['sent']) && $_GET['sent'] == 1) {
echo "<b>Message has been sent</b>";
}
?>
Related
I have two websites, each with it's own domain name. On load I have execute a php file that is used on both sites and has the same functionality.
Here is the code of that file:
<?php
echo '1';
if ($_SESSION["template"]=="template1";)
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template1/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template1/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template1/c.php');
}
else if ($_SESSION["template"]=="template2";)
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template2/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template2/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template2/c.php');
}
else {
echo 'NO DATA';
}
echo '4';
?>
On each of the two sites I set a session variable but in the above code it doesn't seem to work as I expect it to.
Am i missing something?
remove semicolon from if() and else if() statement, also add brackets when you using nested if else because it makes someone to understand easier and looks better
<?php
echo '1';
if ($_SESSION["template"]=="template1")
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template1/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template1/b.php');
}
else if { ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template1/c.php');
}
}
else if ($_SESSION["template"]=="template2")
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template2/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template2/b.php');
}
else if ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template2/c.php');
}
}
else {
echo 'NO DATA';
}
echo '4';
?>
After reviewing your code I edited my answer. The below code will do exactly the same as your code but requires alot less code.
<?php
if (isset($_SESSION['template']) && isset($_REQUEST['cmdAction'])) {
echo file_get_contents('http://localhost/images/'.$_SESSION['template'].'/'.strtolower($_REQUEST['cmdAction']).'.php');
} else {
echo 'NO DATA';
}
?>
I have a simple PHP code, as below.
When I try the URL localhost/df.php?result1=bharat, I get the result Bharat, exactly as I want it. But when I try the URL localhost/df.php?result2=bharat, I get an error, meaning my result2 variable was not read like my result1 variable did.
Could you please correct my code so that it works?
<?php
if(isset($_GET['Result1']))
{
$file = $_GET['Result1'];
}
else
{
echo "Error"; exit;
}
echo "$result1";
?>
elseif(isset($_GET['Result2']))
{
$file = $_GET['Result2'];
}
else
{
echo "Error"; exit;
}
echo "$result2";
?>
You have way too many errors in your code. The following is the solution to your problem:
<?php
if(isset($_GET['result1']))
{
$result1 = $_GET['result1'];
echo $result1;
}
elseif(isset($_GET['result2']))
{
$result2 = $_GET['result2'];
echo $result2;
}
else
{
echo "Error";
exit();
}
?>
For the future, I would recommend you to learn PHP and be familiar with the basic syntax, at least, before posting questions about it here.
My problem is that htmlspecialchars is not answering when sending email.
So when I enter contact.php?reklamtion=yes (for example), everything looks fine.
echo $formproc gives me ?reklamation=yes.
What should happen in this case is that a mail should be sent to everytime#example.com and reklamation#example.com.
Instead what happens is that a mail is sent to everytime#example.com and fail#example.com, and when redirected $formproc echoes ?none=yes.
Do anyone know what's wrong here, or where I should start looking?
<?php
$frompage = "";
$formproc->AddRecipient('everytime#example.com');
if (htmlspecialchars($_GET["reception"])=="yes") {
$formproc->AddRecipient('reception#example.com');
$frompage = "&reception=yes";
} elseif (htmlspecialchars($_GET["reklamation"])=="yes") {
$formproc->AddRecipient('reklamation#example.com');
$frompage = "&reklamation=yes";
} elseif (htmlspecialchars($_GET["contact1"])=="yes") {
$formproc->AddRecipient('contact1#example.com');
$frompage = "&contact1=yes";
} elseif (htmlspecialchars($_GET["contact2"])=="yes") {
$formproc->AddRecipient('contact2#example.com');
$frompage = "&contact2=yes";
} else {
$formproc->AddRecipient('fail#example.com');
$frompage = "&none=yes";
}
if(isset($_POST['submitted']))
{
if($formproc->ProcessForm())
{
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$formproc->RedirectToURL(" $actual_link?sent=yes$frompage ");
}
}
?>
...
<html>
<body>
<?php
echo $formproc
?>
</body>
</html>
UPDATE:
if(isset($_POST['submitted']))
{
if($formproc->ProcessForm())
{
die($frompage); // This gives back &none=yes on submit
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$formproc->RedirectToURL(" $actual_link?sent=yes$frompage ");
}
}
I was wondering if it's possible to have an if statement within an echo.
I have if statement which works fine when echoing results through the a while loop... This is the statement:
<div><?php if ($row['image'] == '') {}
else {echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";} ?>
<?php if ($row['video'] == '') {}
else {echo "<iframe src={$row['video']}></iframe>";} ?></div>`
So basically it's either a video or an image which works fine but then I implemented an infinite scroll to my blog which echoes the data from the database through and if statement like so:
if ($results) {
while($obj = $results->fetch_object())
{
echo '
<div><h3>'.$obj->headline.'</h3> </div>
<div><img src='data:image/jpeg;base64,".base64_encode('.$obj->image.')."'></div>'
So I wondering if anyone knows if it's possible to transfer that if statement within this echo so that it display an image firstly and then knows whether one is present or when a video is present within the database.
Thanks in advance for any help.
PS: I'm very new to coding/php!
Of course. Just split up the echo into multiple statements:
while($row = $results->fetch_object()) {
echo '<div>';
if ($row['image'] == '') {
} else {
echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";
}
if ($row['video'] == '') {
} else {
echo "<iframe src={$row['video']}></iframe>";
}
echo '</div>';
}
Try this one.
//first initialize a variable as a string
$result="";
while($obj = $results->fetch_object()) {
$result.="<div>";
if (!empty($obj['image'])){
$result.="<img src='data:image/jpeg;base64,".base64_encode($obj['image'])."'>";
}
elseif (!empty($obj['video'])){
$result.="<iframe src={$obj['video']}></iframe>";
}else{
//show some notification or leave it
//echo 'not Found';
}
$result.="</div>";
}
//finally you need to print the result variable.
echo $result;
I'm looking for a function like and if else statement for php which will execute certain html code.
For example:
<?php>
$result = 1;
if ($result == 1)
<?>
html code
else
html code
So, based off the result variable gotten from php scripts, a certain html page is output. I've tried echoing the entire html page, but it just displays the html code-> tags and such.
Hopefully you get what I'm trying to get across, ask if you need any clarification questions. Thanks!
That should work:
<?php
$result = 1;
if($result==1) {
?>
html code
<?php
} else {
?>
html code
<?php
}
?>
The problem I'm facing with the if else statement, is in order to display the html, I have to exit php coding. Thus, the if else statement will not work. (Link)
This is not entirely true. You can use the approach below:
<?php
// Do evaluations
if ( $result == "something" )
{
?>
<p>Example HTML code</p>
<?php
} elseif ( $result == "another thing")
{
?>
<span>Different HTML code</p>
<?php
} else {
?>
<h4>Foobar.</h4>
<?php
}
// Rest of the PHP code
?>
Or, if you don't want to exit PHP coding, you can use the echo or print statements. Example:
<?php
// Evaluations
if ( $result == "foo" )
{
echo "<p>Bar.</p>";
} else {
echo "<h4>Baz</p>";
}
// Some else PHP code
?>
Just be careful with proper sequences of ' and " characters. If your HTML tags are to have arguments, you should watch your step and use either of the following approaches:
echo "<span class=\"foo\">bar</span>";
echo '<span class="foo">bar</span>";
If you want to evaluate some PHP and print the HTML results later, you could use something like this
<?php
$output = "";
if ( $result == "something" ) {
$output = '<p>Example HTML code</p>';
} else if ( $result == "another thing") {
$output = '<span>Different HTML code</p>';
} else {
$output = '<h4>Foobar.</h4>';
}
// Output wherever you like
echo $output;
?>
EDIT (because I'm not sure what you;re trying to do so i'm just putting out different ideas):
If you're trying to output an entire page, it may be useful to use header('location: newPage.html'); instead of $output. This redirects the browser to an entirely new web page. Or you can likely include newPage.html as well.
very close:
<?php
$result = 1;
if ($result == 1){
?>
html code
<?php } //close if
else {
?>
html code
<?php
} //close else
?>
you can echo html code something like this
<?php
$result = 1;
if ($result == 1){
echo "<h1>I love using PHP!</h1>";
}
?>
this would output if Result is 1
**I love using PHP!** //but slightly bigger since its H1