I have a problem here saving a value on a textbox using session.
I enter a value on one of the textbox and the other one has none, I submit it. The value will come out on the first textbox and will disable the textbox, but when i put a value on the second textbox and submit it, the value on the first textbox has gone. Can anyone help me with this problem?
PHP
<?php
session_start();
#$two = $_POST['two'];
#$four = $_POST['four'];
if(isset($_POST['submit'])){
$_SESSION['two'] = $two;
$_SESSION['four'] = $four;
}
?>
HTML
<form method="POST">
Textbox One: <input type="text" value="<?=#$_SESSION['two']; ?>" name="two" class="inputborder" size="20" <?phpif(#$_SESSION['two']){ echo"disabled=disabled";} else{echo ""; }?> />
<br>
Textbox Two: <input type="text" value="<?=#$_SESSION['four']; ?>" name="four" class="inputborder" size="20" <?php if(#$_SESSION['four']){ echo"disabled=disabled";}else{ echo ""; }?> />
<br>
<input type="submit" name="submit">
</form>
Why are you trying to grab the variables for $two and $four before checking submit?
<?php
session_start();
if(isset($_POST['submit'])){
if(isset($_POST['two'])){
$two = $_POST['two'];
}
if(isset($_POST['four'])){
$four = $_POST['four'];
}
$_SESSION['two'] = $two;
$_SESSION['four'] = $four;
}
?>
That should remove the need for the '#' symbol in the PHP. (although further error handling would be appropriate). But you are suppressing errors everywhere, how are you supposed to get feedback?
Related
I want to pass two variable from POST, one is the text I write and the other one is the result of a query with I already have.But for some reason I am not getting the variable values. Can you help me?
This is my first page:
<form method="post" action="EliminarGrupos.php">
<label for="nomegrupo"><b>Editar nome do grupo 1 :</label</b><br>
<?php
while ($row = mysqli_fetch_array($result66)){
$result = $row['titulogrupo'];
$_POST['nomegrupo'] = $result; //saving first variable
?>
<input type="text" placeholder="<?php echo $result?>" name="grupo1" id="velhas"></td> //saving second variable
<?php } ?>
<input type="submit" name="submit_x" data-inline="true" value="Submeter">
</form>
This is my second page where I want the variables to appear
$variable = $_POST['nomegrupo'];
$variable2 = $_POST['grupo1'];
The placeholder attribute is for display purposes only. You need to set the value attribute to have it sent to the server.
To send a second value, just use a second <input> element. If you don't want it visible, set type attribute to hidden.
In addition, you are expecting an associative array from mysqli_fetch_array() which is not going to happen. Your HTML had a number of errors in it, which I think I've fixed below. You always need to escape output with htmlspecialchars(). You should separate your HTML and your PHP as much as possible.
<?php
$row = mysqli_fetch_assoc($result66);
$titulogrupo = htmlspecialchars($row["titulogrupo"]);
?>
<form method="post" action="EliminarGrupos.php">
<label for="velhas"><b>Editar nome do grupo 1 :</b></label><br/>
<input type="text" placeholder="" name="grupo1" id="velhas"/>
<input type="hidden" name="nomegrupo" value="<?=$titulogrupo?>"/>
<button type="submit" name="submit_x" data-inline="true">Submeter</button>
</form>
You get the $_POST data from the form submission, specfically from the name attributes. This is what gives the $_POST its information, which it retrieves from value, not placeholder, as you have it now.
<input name="grupo1" value="one"> will make $_POST['grupo1'] equal to one.
You also shouldn't set the $_POST variable on page 1 as you are currently doing, and should make the unchanged variable from the database call a hidden field:
Page 1:
<form method="post" action="EliminarGrupos.php">
<label for="nomegrupo"><b>Editar nome do grupo 1 :</label>
<?php
while ($row = mysqli_fetch_array($result66)){
$result = $row['titulogrupo'];
?>
<input type="text" value="<?php echo $result; ?>" name="grupo1" id="grupo1">
<input type="hidden" value="<?php echo $result; ?>" name="titlogrupo" id="titlogrupo">
<?php } ?>
<input type="submit" name="submit_x" data-inline="true" value="Submeter">
</form>
Page 2:
$variable1 = $_POST['titulogrupo']; // $row['titulogrupo']
$variable2 = $_POST['grupo1']; // Form input
Hope this helps! :)
I'm learning PHP and trying to understand the if .. else statements a little better, so I'm creating a little quiz. However, I have come across an issue and I don't seem to know what the issue is. My problem is that whenever I type in the age in the input area, it will give me the $yes variable every time even if I enter the wrong age.
Here is my code so far:
My html file:
<form action="questions.php" method="post">
<p>How old is Kenny?<input></input>
<input type="submit" name="age" value="Submit"/>
</p></form>
My php file:
<?php
$age = 25;
$yes = "Awesome! Congrats!";
$no = "haha try again";
if ($age == 25){
echo "$yes";
}else{
echo "$no";
}
?>
You catch the user input inside the $_POST superglobal var (because the method of your form is POST.
So
<?php
$age = 25;
should be
<?php
$age = $_POST['age'];
There is an error in HTML too. This
<input type="submit" name="age" value="Submit"/>
should be
<input type="text" name="age" value=""/>
<input type="submit" value="Click to submit"/>
Because you want one input and one button. So one html element for each element.
and <input></input> must be cleared because it's not valid syntax :-)
<form action="questions.php" method="post">
<p>How old is Kenny?</p><input type="text" name="age"></input>
<input type="submit" value="Submit"/>
</form>
$age = (int) $_POST["age"];
$yes = "Awesome! Congrats!";
$no = "haha try again";
if ($age == 25) {
echo $yes;
} else {
echo $no;
}
<?php
/* Test that the request is made via POST and that the age has been submitted too */
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['age'] ) ){
/*
ensure the age is an integer rather than a string ..
though for this not overly important
*/
$age=intval( $_POST['age'] );
if( $age==25 ) echo "Congratulations";
else echo "Bad luck!";
}
?>
<form action="questions.php" method="post">
<p>How old is Kenny?
<input type='text' name='age' placeholder='eg: 16' />
<input type="submit" value="Submit" />
</p>
</form>
A simple html form, note that the submit button does not carry the values you want to process, they are supplied via the input text element.
First of all, you need to echo the variable; echoing "$no" will keep it as a string. Remove the quotes from "$no" and "$yes" in your if then statement. Otherwise, your code seems sound!
I have a form which submits data, and as a test I am attempting to first check if a variable is set on submit. If the variable is set, a text will be displayed stating something like "Variable is set". However if it isn't set, a text will be displayed saying "variable not set", once the variable hasn't been set it should then be set so next time when the form is submitted it displays variable is set however this is not working for me, for some reason it always displays that the variable is not set, my PHP code will be below:
<?php
if (isset($test)) {
echo "This var is set";
}
if (!isset($test)) {
echo "This var is not set";
$test = 'set';
}
?>
<form action="" method="post">
<input type="text" id="text" name="text" autocomplete="off"><br><br>
<input type="submit" value="submit">
</form>
I feel really stupid for not being able to do something which seems so easy, I am only just learning and sort of trying to teach myself, thank you for any help provided!!!
Working code and explanation:
<?php
$test="";
if (isset($_POST["text"])) { //always directly check $_POST,$_GET var without assigning
echo "This var is set";
$test=$_POST["text"]; // then assign
}
else{ // and use else clause for otherwise case
echo "This var is not set";
$test = 'set'; // AND if you want set to default/custom value in case of not set.
}
?>
<form action="" method="post">
<input type="text" id="text" name="text" autocomplete="off">
<br /><br />
<input type="submit" value="submit">
</form>
If you are using form to submit values, then try this one,
if (isset($_POST['text'])) {
echo "This var is set";
}
if (!isset($_POST['text'])) {
echo "This var is not set";
$test = 'set';
}
Otherwise, If a variable set to empty value like $test = '';
(It means variable is set but it has no values) It will execute your first if condition only.
<?php
$test=$_GET["text"];
if (isset($test)) {
echo "This var is set";
}
if (!isset($test)) {
echo "This var is not set";
$test = 'set';
}
?>
<form action="#" method="get">
<input type="text" id="text" name="text" autocomplete="off"><br><br>
<input type="submit" value="submit">
</form>
You haven't declared the variable $test.
Unless you've still got a bit of PHP somewhere that you haven't included here, your variable is empty. When a form is submitted, the input will be added to either the $_POST array (for method = "post") or else the $_GET array (for method = "get").
To Fix:
<?php
if (isset($_POST['text'])) {
$test = $_POST['text'];
echo "This var is set";
}
if (!isset($_POST['text'])) {
echo "This var is not set";
$test = 'set';
}
?>
<form action="" method="post">
<input type="text" id="text" name="text" autocomplete="off"><br><br>
<input type="submit" value="submit">
</form>
I am making a form in html. When a person clicks on submit, it checks if certain fields are filled correctly, so pretty simple form so far.
However, i want to save the text which is typed into the fields, if a person refreshes the page. So if the page is refreshed, the text is still in the fields.
I am trying to achieve this using php and a cookie.
// Cookie
$saved_info = array();
$saved_infos = isset($_COOKIE['offer_saved_info']) ? explode('][',
$_COOKIE['offer_saved_info']) : array();
foreach($saved_infos as $info)
{
$info_ = trim($info, '[]');
$parts = explode('|', $info_);
$saved_info[$parts[0]] = $parts[1];
}
if(isset($_SESSION['webhipster_ask']['headline']))
$saved_info['headline'] = $_SESSION['webhipster_ask']['headline'];
// End Cookie
and now for the form input field:
<div id="headlineinput"><input type="text" id="headline"
value="<?php echo isset($_SESSION['webhipster_ask']['headline']) ?
$_SESSION['webhipster_ask'] ['headline'] : ''; ?>"
tabindex="1" size="20" name="headline" /></div>
I am new at using SESSION within php, so my quesiton is:
Is there a simpler way of achieving this without using a cookie like above?
Or what have i done wrong in the above mentioned code?
First thing is I'm pretty sure you're echo should have round brackets around it like:
echo (isset($_SESSION['webhipster_ask']['headline']) ? value : value)
That's not really the only question your asking though I think.
If you're submitting the data via a form, why not validate using the form values, and use the form values in your html input value. I would only store them to my session once I had validated the data and moved on.
For example:
<?php
session_start();
$errors=array();
if($_POST['doSubmit']=='yes')
{
//validate all $_POST values
if(!empty($_POST['headline']))
{
$errors[]="Your headline is empty";
}
if(!empty($_POST['something_else']))
{
$errors[]="Your other field is empty";
}
if(empty($errors))
{
//everything is validated
$_SESSION['form_values']=$_POST; //put your entire validated post array into a session, you could do this another way, just for simplicity sake here
header("Location: wherever.php");
}
}
if(!empty($errors))
{
foreach($errors as $val)
{
echo "<div style='color: red;'>".$val."</div>";
}
}
?>
<!-- This form submits to its own page //-->
<form name="whatever" id="whatever" method="post">
<input type="hidden" name="doSubmit" id="doSubmit" value="yes" />
<div id="headlineinput">
<input type="text" id="headline" value="<?php echo $_POST['headline'];?>" tabindex="1" size="20" name="headline" />
<!-- the line above does not need an isset, because if it is not set, it will simply not have anything in it //-->
</div>
<input type="submit" value="submit" />
</form>
I have variables $a and $location. However when I press the 'Submit' button the variable has gone out of scope. I can pass in in the variable from input box T2 but I can't manage to manage to do it to 'a' which is a regular variable. I added echo $b to debug it and I do get that output which verifies the conditional statement is true.
<?php
[...]
$a = '5';
$location = 'home';
if(isset($_POST['submit'])) {
$location=$_POST['T2'];
echo $location;
echo $a;
$b = '6';
echo $b;
[...]
}
?>
<input type="text" name="T2" value="<?php echo $location; ?>">
<button type="submit" name="submit" value="create">Submit</button>
$a="Testing";
echo $a;
if(isset($_POST['submit'])) { // you're not getting here
$location=$_POST['T2'];
echo $a;
echo "Testing one two three"; // even this won't show :)
}
There is only one plausible reason for that second echo not to work, i.e. Your if condition doesnt evaluate to true. There is no other reason that can cause that. Now check your field or button named submit on the form :)
A simple print_r($_POST); will tell you all that was posted, you can check there if submit was also posted.
Far as I can tell, you're missing the form tags along with the method which should be post, least from what you posted for code.
Sidenote: If what you posted isn't full code, do. The following works which printed home56 on submit.
I do believe that's what the expected result should be.
<?php
$a = '5';
$location = 'home';
if(isset($_POST['submit'])) {
$location=$_POST['T2'];
echo $location;
echo $a;
$b = '6';
echo $b;
}
?>
<form action="" method = "post">
<input type="text" name="T2" value="<?php echo $location; ?>">
<input type="submit" name="submit" value="create">
</form>
Sidenote: You can keep your present button if you wish instead of the input I tested with:
<button type="submit" name="submit" value="create">Submit</button>
Footnotes:
Both [...] - any relevance? I'll bet there is.
Got it to work by echoing it in a hidden input
<input type="hidden" name="myvar" value="<?php echo $a; ?>">
and then using $_POST to extract it
$a=$_POST['myvar'];