print a variable generated from another .php file - php

I'm trying to display the value of $codeOut from a php file called get_rebate.php.
Currently I am using:
<?php include ("get_rebate.php"); echo substr($codeOut,7); ?>
to try and get the output of $codeOut from get_rebate.php to appear in the page but instead it returns the error message that get_rebate.php generates when it can't verify the value.
I'm probably not calling the result of get_rebate.php ($codeOut) properly but looking for guidance with this problem.
get_rebate.php:
<?
$code=$_GET['code'];
$rnum=50000-$_GET['rnum'];
if ($code=="2210" || $code==$rnum) {
makeVerifier($code);
} else { echo "The Rebate Code you have entered (" . $code . ") does not apply to this product.\nPlease consult with your Fitness Expert and request if a Rebate Code\nis available for this product.";
}
function makeVerifier($codeIn) {
$len=strlen($codeIn);
for ($i=0;$i<$len;$i++) {
$codeOut.=ord(substr($codeIn,$i));
if (strlen($codeOut)>7) {break;}
}
echo "Congratulations! Your Manufacturers Rebate Verification Code is M" . substr($codeOut,0,7);
}
?>
The output on the page is: The Rebate Code you have entered () does not apply to this product. Please consult with your Fitness Expert and request if a Rebate Code is available for this product

Obviously $code is not 2210 or $rnum so you get the error message. Even if the code would be correct, it wouldn't work: $codeOut's scope is limited to the makeVerifier() function. You need to call it with $codeOut = makeVerifier( $code ) and at the end of the function add return $codeOut.

Related

How to get current url and get code from it in PHP

Im making an email verification system on PHP in cPanel, and when i press register, it sends an email to me with a link like this "../verified.php?code=1cbb402a59e8ec26dac0", i would need to get the full link and would have to chop it so it leaves me with just the code "1cbb402a59e8ec26dac0" so i can check if the code exists in database and then verify the account.
So from this
../verified.php?code=1cbb402a59e8ec26dac0
To This
"1cbb402a59e8ec26dac0
Purchasing the hostings for the first time fried my brains, so would be thankful if anyone could help me,
For getting the text after the code: in the link, you can use the PHP $_GET function. You can use this code in your verified.php to get the text after code:
<?php
if(isset($_GET['code']))
{
$Data=$_GET['code'];
}
else{
echo "Invalid Email Verification";
?>
Now the part after the code= gets stored in the variable Data.
You can change that to your desired variable.
Sometimes even when the code is set, it might be empty, so to check that, the empty() function in PHP can be used.
You can add this code to your verified.php:
<?php
if (empty($Data))
{
echo "Invalid Verification ID";
}
else
{
echo "Email Verification Success";
//your other codes to update it on the server
}
?>
Well you have chosen to pass parameters which will be available in the global $_GET[] (as opposed to $_POST[]).
So in your verified.php you will need to examine for the presence of $_GET['code'] and take the appropriate action.
Using $code = $_GET['code']; is very bad as you need to qualify it.
At a minimum you would need to...
<?php
// Ternary to give $code a value to prevent undefined variable error.
$code = isset($_GET['code']) ? $_GET['code'] : NULL;
if($code === NULL){
// error
} else {
// Check its a valid code and take the appropriate action.
}

echo isset with condition

When people search for a real estate agent by zip code they will see a message on the site I'm working on that reads: There are x number of our Agents in your neighborhood.
x is the number determined by this php code:
<?php echo isset($total_record) ? $total_record : "";?>
if the number is Zero, the message sounds dumb (There are 0 number of...)
How do I change the message just for those cases with 0 as a search result? so that a different message appears? Something like - Sorry, we don't have any Agent in your immediate area.
Any help, much much appreciated.
Use a simple if statement:
if (isset($total_record) && $total_record > 0){
echo $total_record." number of our Agents in your neighborhood";
} else {
echo "Sorry, we don't have any Agent in your immediate area.";
}
if (isset($total_record))
{
if ($total_record > 0)
{
echo "There are {$total_record} of our Agents in your neighborhood.";
}
else
{
echo "There are no Agents in your neighborhood.";
}
}
Use the empty function rather than isset. The empty function checks if a variable exists and has a value. 0, false, and a few other values are also considered empty, check the manual for a full listing.
echo !empty($total_record) ? 'There are ' . $total_record . ' number of our Agents in your neighborhood.' : 'Sorry, we don\'t have any Agent in your immediate area.';

Using if else statement based off of $_POST value to echo message

Pretty sure this is a quick and easy question but I have a form that on action POST goes to a confirmation page. I need a message to display on the confirmation page if the user selects county1 but if user selects county2, county3, or county4. However, when I setup the statement it's not working. Probably a syntax error or two on my part. Any help would be greatly appreciated.
A messy idea of what I think should work:
<?php $county=$_POST['County'];
if ($county="Polk") {
echo "Important message about your county"; }
else {
echo " "; // Or nothing at all
}
?>
But
<?php echo $_POST['County'] ?>
displays the name of the county so I know the submission is carrying through. Thoughts on why my above code wouldn't be working? If you could flag syntax errors or code placement that'd be much appreciated! Thank you!
Inside the if condition you should use two equal operators instead of one . try this code
<?php
$county = isset($_POST['County'])?$_POST['County']:"";
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
?>
Use double equal in your if statement for comparison
See the answer and read the comment to understand why you have to change it
if ($county="Polk") {
Single equal is assignment operator, it assign value
Change this line to this
if ($county=="Polk") {
So your whole code should look like this
$county=$_POST['County'];
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
Use double equal sign, double equal is comparison operator,
Here you are checking if the $county is equal to Polk or not,
Means you are comparing value

How to display empty field error message

I have a page which allows the user to "create a topic", open submitting this the form goes to another through a verification process which inserts the topic into the database and re-directs to back to the main page. However I want my verification page "add topic" to display an error message if all fields are not filled in. here is a my code, please can you tell me where I would need to add this validation code to notify the user to fill all fields:
// get data that sent from form
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_POST['name'];
$email=$_POST['email'];
$datetime=date("d/m/y h:i:s"); //create date time
$sql="INSERT INTO $tbl_name(topic, detail, name, email, datetime)VALUES('$topic', '$detail', '$name', '$email', '$datetime')";
$result=mysql_query($sql);
if($result){
echo "Successful<BR>";
echo "<a href=main_forum.php>View your topic</a>";
}
else {
echo "ERROR";
}
mysql_close();
My suggestion would be create a separate php file called validation and inside the validation file add a function. Of course you can create this function inside the same php file. If you made the separate use an include statement to place it on your page. Also a quick post-back to itself would be good since you could easily be able to get access to the posted variables and already be on the page to show errors. Otherwise you would have to return the Errors in a get, post or session. If everything was successful you could post or redirect right after the postback (maybe to a success page) and the user would only see the postback if errors present.
include_once("Validation.php");
as shown above.
validateNewTopic($topic, $detail, $name, $email, $datetime)
{
}
Then inside you could use if statements to check conditions. If you want a quick solution you can create a variable to hold all the errors.
$Error = "<p class='errors'">;
if ($topic == "")
{
$Error+="topic is required";
}
if ($Error != "<p class='errors'">)
{
return $Error +"</p>";
}
else
{
return "";
}
Since you are posting the values you can catch them in a variable on postback to validate.
$topic = $POST['topic'];
$Error=validateNewTopic($topic);
if ($Error != "")
{
?>
echo $Error
<?php
}
else {
//run sql code and show success
}
By putting the paragraph tags inside the $Error messages we can just echo and it will already be in the paragraph tag with the class errors. You can make it prettier by using an un-ordered list and when adding an error using list items. I'm not sure how familiar you are with php but at anytime you can stop writing php code by closing the tags. (< php ?> and reopen < ? php) as shown above in the if statement. I know this was not 100% clear but this is something you should try/research and practice since it is used so often. Good luck!
You can send the error to the main page by using php GET request, and then display it.

How to check if the user has entered or not entered data to Mysql using PHP?

I'm trying to see if the user has entered a website URL into the database with the following code. If the user did not enter their website's URL, do not display anything. If the user did enter their website, display the website.
I think I'm doing it wrong.
<?php
if (!empty($url))
{
echo'';
} else {
echo'p>Website: <?php echo "http://","$url"; ?></p>';
}
?>
Apart from the wrong order (you need to remove ! before the empty()), you also have some errors in the echo part: the < is missing for the < p > tag and you are including php tags in your string.
It should read something like:
echo '<p>Website: http://' . $url . '</p>';
what you've got there is that you're checking if it's NOT empty (that is, there is some data), you're echoing an empty string. If it IS empty, then you're echoing it out.
Remove the ! in the first line and you should be right.
You're simple mismatching the meaning of "!" :
<?php
if (!empty($url))
{
echo'p>Website: <?php echo "http://","$url"; ?></p>';
} else {
echo'';
}
?>

Categories