Can I pass a variable in a Meta Http Equiv Refresh? - php

I just finished this script it works well, but I am struggling to pass along the $invite variable to the register.php page. Can someone please tell me how I can achieve this? I have it defined as $invite = $_POST['inviteinput'];
How do I get the users input from index.php to transfer to register.php? Thank you for your help!
HTML FORM:
<form action="index.php" method="post">
What is your Invite Code?<BR />
<input class="textbox" name="inviteinput" type="text" />
<BR />
<input type="hidden" name="formsubmitted" value="TRUE" />
<input name="Submit" type="submit" value="Verify" />
</form>
PHP:
<?PHP
include ('scripts/dbconnect.php');
if (isset($_POST['formsubmitted'])) {
if (empty($_POST['inviteinput'])) {
echo '<div class="errormsgempty"><u>ERROR</u>:<br>You must enter a valid invite code to proceed!</div>';
} else {
$invite = $_POST['inviteinput'];//else assign it a variable
$invite = stripslashes($invite);
$invite = mysql_real_escape_string($invite);
$sql = "SELECT yourMemberId FROM Register WHERE yourMemberId='$invite'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=register.php">';
} else {
echo '<div class="errormsgbox"><u>ERROR</u>:<br>The Invite Code you entered ' . $invite . ' is NOT a valid invite code!</div>';
//($notvalidcode = "The Invite Code you entered <strong>" . $invite . "</strong> is NOT a valid invite code!");
}
}
}
?>

Absolutely. Just add it to the URL:
<meta http-equiv="Refresh" Content="0; url=register.php?var=value&othervar=hello" />
Note that you may be better off using:
header("Location: register.php?var=value&othervar=hello");
exit;

There are a few ways
Save it in a session
session_start();
$_SESSION['invite'] = $invite;
2nd is you can pass it via get
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=register.php?invite=' . $invite . '">';
I would recommend option 2.
Also, why are you doing $invite = $_POST['inviteinput']; twice?

Related

Printing the history using cookies in PHP

<html>
<head>
<meta charset="UTF-8">
<title> Cookies </title>
</head>
<body>
<h1> Cookies Concept </h1>
<form method="get" action="index.php">
Enter Your Name: <input type="text" name="name">
<input type="submit" name="done">
</form>
</body>
</html>
<?php
if(!empty($_GET['name']))
{
if(empty($_COOKIE['name']))
{
setcookie('name',$_GET['name']."<br",time()+86400);
}
else
{
setcookie('name',$_GET['name'].<br>".$_COOKIE['name'],time()+86400);
}
}
if(isset($_COOKIE['name']))
{
echo $_COOKIE['name'];
}
else
{
echo "Cookie cannot be set";
}
?>
I want to print the last ten name entered . How to do this I don't know please help me ?
If you want to save the last 10 from the same user you can use serialize to save an array in the cookie. But remember that will not share info between users since cookie is just for that visitor.
For example:
if(isset($_GET['name'])){ #get the name
$name = strip_tags($_GET['name']);
$names = []; # just names in case there is no names array
if(isset($_COOKIE['cookie'])){ #read cookie
$names = unserialize($_COOKIE['names']);
}
array_unshift($names, $name); #put the name in begging of the list
if(count($names) > 10 ){ #remove last entry if have more then 10
array_pop($names);
}
setcookie('names', serialize($names), time()+3600);
}
//to print just read cookie and
foreach($names as $name ){
echo $name;
}

Can't obtain value from form in another PHP page after sending it

firstly, I'm pasting my code and will explain after it what my issue is.
Index.php
<?php
$userPassword=array(
'paul'=>'123456',
'chiri'=>'hola',
'maria'=>'adios'
);
$nameUser='';
$passwordUser='';
$errorMessage='';
function firstExecution(){
if(isset($_SERVER['HTTP_REFERER'])){
if(strpos($_SERVER['HTTP_REFERER'],$_SERVER['PHP_SELF'])!=FALSE){
return false;
}else{
return true;
}
}else{
return true;
}
}
if(firstExecution()==false){
$nameUser=$_GET['user'];
$passwordUser=$_GET['password'];
$errorMessage="Wrong data";
while($cred = current($userPassword)) {
if($cred == $passwordUser) {
if(key($userPassword) == $nameUser) {
header('Location: counter.php');
break;
} else {
$errorMessage='Wrong data';
break;
}
} else {
$errorMessage='Wrong data';
break;
}
}
}
?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div id="errors" style="color: red;"><?php echo $errorMessage; ?></div>
Name: <input type="text" name="user" value=""/>
<br/>
Password: <input type="text" name="password" value=""/>
<br/>
<input type="submit" name="send" value="send"/>
</form>
</body>
</html>
Counter.php
<?php
error_reporting(0);
$cookieName=$_GET['user'];
$cookieValue=$_GET['Jobs'];
/*We create the array with Values for Radio Button group*/
$arrayJobs=array(
'Programmer'=>'Programmer',
'Lawyer'=>'Lawyer',
'Doctor'=>'Doctor'
);
/*Function to create Radio Buttons*/
function generateRadioButton($name, $valueArray, $selectedValue) {
$exit = '';
foreach ($valueArray as $key => $value) {
if ($selectedValue == $key) {
$exit .= '<label>' . $value . '</label><input type="radio" name="'. $name .'" value="' . $key . '" checked/>' . PHP_EOL;
} else {
$exit .= '<label>' . $value . '</label><input type="radio" name="'. $name .'" value="' . $key . '" />' . PHP_EOL;
}
}
return $exit;
}
/*We create the cookie if submit has been clicked and go to Index.php*/
if(isset($_GET['disconnect'])){
setcookie($cookieName, $cookieValue, time()+3*24*3600);
header('Location: index.php');
}
?>
<html>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php echo generateRadioButton('Jobs', $arrayJobs, $cookieValue);?>
<?php echo $cookieName?>
<br>
<input type="submit" name="disconnect" value="Disconnect"/>
</form>
</html>
On index.php I have a login form, and those credentials are stored in an array (we haven't worked with db yet so this is how we have to do this exercise now), if login is wrong it will show an error message, if correct it will go to counter.php; on contador.php I have 3 radio button and a disconnect button, therefore, when clicking on Disconnect it has to create a cookie with the value of the selected radio button and name of the user.
Now, my problem is, the cookie is created but without any name, for some reason I can't find, counter.php does not get the name introduced on index.php; I can imagine it's somehow related to the use of header() but I don't how to keep my restrictions on the login and at the same time send the values of the login to counter.php
counter.php doesn't get the form data because the form data has been sent to index.php and you redirect to counter.php with the header method.
The easiest thing would be to store the submitted data in the session variable.
See http://php.net/manual/en/reserved.variables.session.php
So in index.php right before the header() command, you type: $_SESSION['user'] = $_GET['user']; and in counter.php you type: $user = $_SESSION['user'];

On form submission the browser loads the php file that had the code for handling the form

I'm writing my first php code... I'm trying to submit a form.
I have two php files. index.php(which contains the form) and process.php(which contains the method that handles the form).
But on form submission, the browser heads to process.php, so nothing is displayed.
I'm trying to echo the result in index.php .
And keep in mind this is my very first php code... :-)
This is index.php
<!DOCTYPE html>
<html>
<?php
include 'process.php';
$newletter1 = new newsletter();
?>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="process.php" method="post">
<input type="text" name="email" placeholder="Your Email Address..."><br><br>
<input type="submit">
</form>
<h4><?php $newletter1 -> abc(); ?></h4>
</body>
</html>
And this is process.php
class newsletter
{
public function abc()
{
if (isset($_POST["email"])) {
$input = $_POST["email"];
if (empty($input)) {
echo "Please provide an email address!";
}else{
echo "Thanks for subscribing " . $input;
}
}else{
echo "ELSE is running...";
}
}
}
Your script process.php is just a class definition.
A class does nothing unless it is instantiated and a method called.
As you are including it and instantiating it in your index.php I would suggest changing your <form> tag so it runs itself, leaving href="" will do that.
<?php
// run this only if we are being set info by the user
// so not when the form is first loaded.
$to_show_later = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST' ) {
include 'process.php';
$newletter1 = new newsletter();
$to_show_later = $newsletter1->abc();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="" method="post">
<input type="text" name="email" placeholder="Your Email Address..."><br><br>
<input type="submit">
</form>
<h4><?php echo $to_show_later; ?></h4>
</body>
</html>
It's also bad practice to echo directly out of a class method, so change this so it return the data.
class newsletter
{
public function abc()
{
$reply = '';
if (isset($_POST["email"])) {
$input = $_POST["email"];
if (empty($input)) {
$reply = "Please provide an email address!";
}else{
$reply = "Thanks for subscribing " . $input;
}
}else{
$reply = "ELSE is running...";
}
return $reply;
}
}
In your process.php replace it by:
if (isset($_POST["email"])) {
$input = $_POST["email"];
if (empty($input)) {
echo "Please provide an email address!";
}else{
echo "Thanks for subscribing " . $input;
}
}else{
echo "ELSE is running...";
}
It will work, and you dont have to include process.php to submit form only

php get the hidden value after redirect the page

test.php
<?php
//CLICK SUBMIT BUTTON
if(isset($_POST['submit']))
{
$membername = $_POST['membername'];
$errors = '';
if(empty($membername))
{
$errors = "Please enter member name!<br />";
}
if($errors)
{
//MEMBER NAME TEXTFIELD EMPTY
//SHOW ERROR MESSAGE AND DISPLAY FORM AGAIN
echo '<span style="color:red;font-weight: bold;">'.$errors.'</span>';
displayForm();
}
else
{
//GO TO OUTPUT.PHP PAGE
header("Location:output.php");
exit();
}
}
else
{
displayForm();
}
?>
<?php
//DISPLAY FORM
function displayForm()
{
?>
<html>
<head></head>
<body>
<form action="test.php" method="post">
Member Name
<input type="text" name="membername" value="<?php if(isset($_POST['membername'])) echo $_POST['membername'];
else echo ''; ?>" /><br />
<input type="submit" name="submit" value="add" />
[HERE]
</form>
</body>
</html>
<?php
}
?>
In [HERE] section, I write the hidden input field:
<input type="hidden" name="mname" value="<?php echo $_POST['membername']; " />
After that, I go to output.php get the hidden field value:
<?php
echo $_POST['mname'];
?>
When I run the code, I get this error:
Undefined index: mname
What happened to my program?
header() function cannot applied to $_POST method?
Any solutions to solve it?
There Could be two solutions to get the value on redirected page :
1. By Session :
You can put the value in session and get on the redirected page.
$_SESSION['mname'] = $_POST['mname'];
2. Using GET :
You can send values in header.
header("Location:output.php?val=$_POST['mname']");

Contact form not working

i can't make this contact form work, not sure what it is, will much appreciate your help. here is the code:
<?php
//Process Contact
if (isset ($_POST['send'])) {
//Variables
$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];
//Check all the inputs
if ($name!='' && $email!='' && $message!='') {
// then Html
$contenido = '<html><body>';
$contenido .= '<h2>Contact from</h2>';
$contenido .= '<p>Sent: '. date("D M Y").'</p>';
$contenido .= '<p>Name: <strong>'.$name.'</strong>';
$contenido .= '<p>Email: <strong>'.$email.'</strong>';
$contenido .= '<p>Message: <strong>'.$message.'</strong>';
$contenido .= '<hr />';
$contenido .= '</body></html>';
// If the forms are full, it shows the message
mail ("mj#marijoing.com", "Mother-Well", $contenido, "From: $email\nContent-Type:text/html; charset=iso-8859-1\nContent-Transfer-Encoding: 8bit");
$flag='MessageSuccess';
$mensaje='<div class="MessageSuccess">Your message has been sent, we will contact you shortly .<br/><strong>Thank you!</strong> </div>';
} else {
//If there's a from to fill...
$flag='err';
$mensaje='<div class="MessageError">All the information in the entry form are required. Please, try again</div>';
}
}
?>
And the HTML:
<? echo $mensaje; /*Status form */ ?>
<? if ($flag!='MessageSuccess') { ?>
<form action="contact.php" method="post">
<input <? if (isset ($flag) && $_POST['name']=='') { echo 'class="MessageError"';}?> type="text" value="<? echo $_POST['name'];?>" maxlength="40" /><br />
<input <? if (isset ($flag) && $_POST['email']=='') { echo 'class="MessageError"';} ?> type="text" value="<? echo $_POST['email'];?>" maxlength="40" /><br />
<textarea <? if (isset ($flag) && $_POST['message']=='') { echo 'class="MessageError"';} ?> name="message" rows="4"><? echo $_POST['message'];?></textarea><br />
<input type="submit" value="CONTACT US" name="send" />
</form>
<? } ?>
Hope somebody can help me, thanks in advance :)
This is by no means the only possible error, but you do
<? if ($flag!='MessageSuccess') { ?>
<form action="contact.php" method="post">
<input <? if (isset ($flag)
In other words, you are assuming the $flag variable exists in the outer loop, then test for its existence in the inner loop. That makes no sense.
Further I would recommend using
<?php
instead of
<?
at all times. Not sure if that makes a difference, but it's certainly more conventional. See https://softwareengineering.stackexchange.com/a/151694 for a description of short tags, and when they are OK to use (did you enable them in your build?)

Categories