PHP get request resets page - php

So, I'm making a PHP page. I'm trying to get a url like this
https://nikkiedev.com/folder/indexfile/?schedule-btn=true&time-zone=CET
but instead, when I click on my GET form for the time zone it goes to
https://nikkiedev.com/folder/indexfile/?time-zone=CET
Here's my code:
<div class='mobile-center'>
<form method="GET">
<button name="schedule-btn" type="submit" value="true" class="btn">schedule</button>
<button name="roles-btn" type="submit" value="true" class="btn">roles</button>
</form>
<?php
$sched_btn = $_GET["schedule-btn"];
$roles_btn = $_GET["roles-btn"];
if (isset($sched_btn)) {
echo "<div class='mobile-center'>";
echo "<h1>Change schedule</h1>";
echo "<hr>";
echo "<p>Choose time zone</p>";
echo "<form method='GET'>";
echo "<p>
<input type='radio' name='time-zone' value='CET'> CET
<input type='radio' name='time-zone' value='EST'> EST
<input type='submit'>
</p>";
echo "</form>";
$timezone = $_GET["time-zone"];
echo "</div>";
if ($timezone == "CET") {
echo "<h1>CET</h1>";
}
else if ($timezone == "EST") {
echo "<h1>EST</h1>";
}
}
?>
</div>

Add the value of $sched_btn as a hidden input in the new form.
if (isset($sched_btn)) {
echo "<div class='mobile-center'>";
echo "<h1>Change schedule</h1>";
echo "<hr>";
echo "<p>Choose time zone</p>";
echo "<form method='GET'>";
echo "<input type='hidden' name='schedule-btn' value='$sched_btn'>";
echo "<p>
<input type='radio' name='time-zone' value='CET'> CET
<input type='radio' name='time-zone' value='EST'> EST
<input type='submit'>
</p>";
echo "</form>";
$timezone = $_GET["time-zone"];
echo "</div>";
if ($timezone == "CET") {
echo "<h1>CET</h1>";
}
else if ($timezone == "EST") {
echo "<h1>EST</h1>";
}
}

Related

How can I display the value that I got from input text field for the following codes?

Hye! can anyone help. I'm stuck already.
I want to display the value that I got from the text field for the following code. Oh!!! the $str in the code actually is the value that I got from the dropdown option.
So actually I'm trying to have a dropdown button on the page which is when the user select a value at first dropdown, another dropdown menu / text box will display. As the codes below, different value may have different display for the next display either dropdown OR text box. So now i'm trying to get the value from the next displayed dropdown OR text box. so for now i'm trying to get the value from the text box that displayed after selection from the first dropdown.
Hope this will be clear :)
Thanks for helping
<form name="nameOfForm" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>" action="search_handler">
<div class="row">
<div class="col-md-4">
<?php
echo "<b>Search By : </b><br>";
echo "<select class='inputStyle' name='option' onchange='this.form.submit();'>";
echo "<option value=''>--Please Select--</option>";
echo "<option value='RefNo'>Reference No</option>";
echo "<option value='Date'>Date</option>";
echo "<option value='Requestor'>Requestor</option>";
echo "<option value='Type'>Type</option>";
echo "</select>";
?>
</div>
<div class="col-md-4">
<?php
$str='';
$maxDate = date("d-m-Y");//todays date
//echo $maxDate;
if($_GET){
//echo $_GET['option'];
$str = $_GET['option'];
//echo $str;
}
if ($str != ''){
if ($str == "RefNo"){
//echo "Hello!";
echo "<b>Type Reference No. Here :</b><br>";
echo "<input class='inputStyle' id='RefNo' name='RefNo' type='text' autocomplete='off' required>";
}else if ($str == "Requestor"){
//echo "Hello!";
echo "<b>Type Requestor Name Here :</b><br>";
echo "<input class='inputStyle' id='ReqName' name='ReqName' type='text' autocomplete='off' required>";
}else if($str == "Type"){
echo "<b>Choose Type :</b><br>";
echo "<select class='inputStyle' name='type'>";
echo "<option value=''>--Please Select--</option>";
echo "<option value='PETTY CASH'>Petty Cash</option>";
echo "<option value='OTHERS'>Others</option>";
echo "</select>";
}else{
//echo "Date Here!";
echo "<b>From</b><br>";
echo "<input class='inputStyle' onchange='allowToDate()' id='from' name='from' type='date' max='<?php echo $maxDate; ?>' required>";
echo "<b>To</b><br>";
echo "<input class='inputStyle' id='to' name='to' type='date' max='<?php echo $maxDate; ?>' required disabled>";
}
}
else {
echo "</br>";
echo "Please Select A Value From Dropdown!";
}
?>
</div>
<div class="col-md-2">
<br>
<input class="submitBtnBS" type="button" onclick="search()" id="proceed" name="proceed" value="PROCEED" style="height:35px; width:100%;">
</div>
<div class="col-md-2">
<br>
<input class="clearBtnBS" type="button" onClick="clearForm(this.form)" id="clear" name="clear" value="CLEAR" style="height:35px; width:100%;">
</div>
</div>
</form>
In your HTML File:
<form action="yourphpfile.php" method="get">
<select name="RefNo" id="RefNo" required class="">
<option value="">Choose one</option>
<option>List 1</option>
<option>List 2</option>
<option>List 3</option>
<option>List 4</option>
</select>
<input type='submit' value='submit' name='submit' />
</form>
In your PHP:
<?php
//To prevent having "Undefined index" warnings, you need to check if GET is the request method
if ($_SERVER['REQUEST_METHOD'] == "GET"){
$str = $_GET['RefNo'];
..
..
..
..
..
}
?>
You should create a form (with post or get method). Then when the form is send to your page you can get the inputed value.
Your code modified:
<form name="nameOfForm" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>" action="search_handler">
<div class="row">
<div class="col-md-4">
<?php
echo "<b>Search By : </b><br>";
echo "<select class='inputStyle' name='option' onchange='this.form.submit();'>";
echo "<option value=''>--Please Select--</option>";
echo "<option value='RefNo'>Reference No</option>";
echo "<option value='Date'>Date</option>";
echo "<option value='Requestor'>Requestor</option>";
echo "<option value='Type'>Type</option>";
echo "</select>";
?>
</div>
<div class="col-md-4">
<?php
$str='';
$maxDate = date("d-m-Y");//todays date
//echo $maxDate;
if($_GET){
//echo $_GET['option'];
$str = $_GET['option'];
//echo $str;
}
if ($str != ''){
if ($str == "RefNo"){
//echo "Hello!";
echo "<b>Type Reference No. Here :</b><br>";
echo "<input class='inputStyle' id='RefNo' name='RefNo' type='text' autocomplete='off' required>";
}else if ($str == "Requestor"){
//echo "Hello!";
echo "<b>Type Requestor Name Here :</b><br>";
echo "<input class='inputStyle' id='ReqName' name='ReqName' type='text' autocomplete='off' required>";
}else if($str == "Type"){
echo "<b>Choose Type :</b><br>";
echo "<select class='inputStyle' name='type'>";
echo "<option value=''>--Please Select--</option>";
echo "<option value='PETTY CASH'>Petty Cash</option>";
echo "<option value='OTHERS'>Others</option>";
echo "</select>";
}else{
//echo "Date Here!";
echo "<b>From</b><br>";
echo "<input class='inputStyle' onchange='allowToDate()' id='from' name='from' type='date' max='<?php echo $maxDate; ?>' required>";
echo "<b>To</b><br>";
echo "<input class='inputStyle' id='to' name='to' type='date' max='<?php echo $maxDate; ?>' required disabled>";
}
}
else {
echo "</br>";
echo "Please Select A Value From Dropdown!";
}
?>
</div>
<div class="col-md-2">
<br>
<input class="submitBtnBS" type="button" onclick="search()" id="proceed" name="proceed" value="PROCEED" style="height:35px; width:100%;">
</div>
<div class="col-md-2">
<br>
<input class="clearBtnBS" type="button" onClick="clearForm(this.form)" id="clear" name="clear" value="CLEAR" style="height:35px; width:100%;">
</div>
</div>
</form>
<?php
if(isset($_GET["RefNo"])){
echo $_GET["RefNo"];
}
if(isset($_GET["ReqName"])){
echo $_GET["ReqName"];
}
?>

posting Php variable without href tag to other file

this is my first file code, where the php code is inside an html form and sending data by post method:
<form name="quiz" action=quizaction.php method="POST">
<?php
$sql = "SELECT * FROM questions WHERE `type` IN
('".implode("','",$fin_element)."')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$index = 0;
while($row = $result->fetch_assoc()) {
echo "<br>";
echo "Q:" . $row["question_name"]. "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.1'/>
<code>".$row["answer1"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.2'/>
<code>".$row["answer2"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.3'/>
<code>".$row["answer3"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.4'/>
<code>".$row["answer4"]."</code>". "<br>";
$index++;
echo $index;
}
} else {
echo "0 results";
}
<INPUT TYPE="SUBMIT" VALUE="Done" >
<INPUT TYPE="RESET" VALUE="Clear all fields of this form">
i want to use the $index variable in other file named quizaction.php file ,how do i ??
Use SESSION
<form name="quiz" action=quizaction.php method="POST">
<?php
session_start();//session starts here
$sql = "SELECT * FROM questions WHERE `type` IN
('".implode("','",$fin_element)."')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$index = 0;
while($row = $result->fetch_assoc()) {
echo "<br>";
echo "Q:" . $row["question_name"]. "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.1'/>
<code>".$row["answer1"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.2'/>
<code>".$row["answer2"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.3'/>
<code>".$row["answer3"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.4'/>
<code>".$row["answer4"]."</code>". "<br>";
$index++;
echo $index;
}
} else {
echo "0 results";
}
// set session value and use $_SESSION['index'] at quizaction.php to find session value
if($index> 0){
$_SESSION['index'] = $index;
}
?>
<INPUT TYPE="SUBMIT" VALUE="Done" >
<INPUT TYPE="RESET" VALUE="Clear all fields of this form">
You can take hidden text-box in your form. then you can get that value in quizaction.php file.
while($row = $result->fetch_assoc()) {
echo "";
echo "Q:" . $row["question_name"]. "";
echo "<input type='radio' name='question".$index."' value='answer1.1'/>
<code>".$row["answer1"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.2'/>
<code>".$row["answer2"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.3'/>
<code>".$row["answer3"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.4'/>
<code>".$row["answer4"]."</code>". "<br>";
echo "<input type='hidden' name='indexid' value='".$index."'/>
$index++;
echo $index;
}

On submit i need to pass anchor tag value in php

while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
echo "<form method='post'><a href='".$row['user_from']."' name='yo' value'".$row['user_from']."'>".$row['user_from']."</a> &nbsp";
echo "<input type='submit' name='acc' value='Accept'> &nbsp <input type='submit' name='cancel' value='Reject Rrquest'></form> <br/><br/>";
}
if(isset($_POST['acc']))
{
}
Blockquote
//on submit here i need to disply corresponding $row['user_from']
value
Use <input type="hidden" name="whateveryouwant" />, if you don't want to display text field to user.
Try this:
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
echo "<form method='post'><input type='hidden' name='user_from' value='".$row['user_from']."' /><a href='".$row['user_from']."' name='yo' value'".$row['user_from']."'>".$row['user_from']."</a> &nbsp";
echo "<input type='submit' name='acc' value='Accept'> &nbsp <input type='submit' name='cancel' value='Reject Rrquest'></form> <br/><br/>";
}
if(isset($_POST['acc']))
{
echo $_POST['user_from'];//echo the value of user_form
}
<?php
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
?>
<form method="post" action="">
<?php $row['user_from'] ?>
<input type="submit" name="acc" value="Accept">
<input type="submit" name="cancel" value="Reject Rrquest">
</form>
<php
}
?>
<?php
if(isset($_POST['acc']) && !empty($_POST['yo']))
{
$link = $_POST['yo'];
// do what you want to do with this `url`
}
?>
NOTE: Don't Complex Your Code With Using Html COde In Php echo. You Can Just Open the While Loop Brackets { and then close the php ?> then simple html you have to written, So Just Avoid to used html code inside the php echo

Form submission doesn't work - PHP

I'm working on a form validation and I'm currently doing some debugging on my code. I'll insert the necessary code snippets below:
Form code:
echo '<h1> Seismic Recording </h1>
<div>
<form action="validate2.php" method="POST">';
echo "Latitude: <input type='text' name='latitude'>";
echo "Longitude: <input type='text' name='longitude'required>";
echo 'Wave Type: <select id="wave_type" name="wave_type" required>
<option selected value="s">S</option>
<option value="p">P</option>
<option value="surface_wave">Surface Wave</option>
</select>';
echo "Wave Value: <input type='text' name='wave_value' required>";
echo 'Upload CSV File: <input type="file" name="file">';
echo " <input type='submit' name='submit' value='submit'>";
echo "</form>
</div> ";
Validation code:
echo "w";
if (isset($_POST['submit'])) {
echo "t";
$latitude = $_POST['latitude'];
if ($latitude == NULL) {
echo "Please insert a latitude . <br>";
$error = $error + 1;
}
}
echo "q";
The form validations that I've put within the if statement doesn't work. I tried debugging the code by inserting the 'echo "q"' and 'echo "w"' to see where the problem lies. Those statements work (they output the characters). But the 'echo "t"' within the if statement doesn't work. Why is that so?
Result of var_dump($_POST):
array(5) { ["latitude"]=> string(0) "" ["longitude"]=> string(1) "g"
["wave_type"]=> string(1) "s" ["wave_value"]=> string(1) "g"
["file"]=> string(0) "" }
My issue isn't so much in getting the latitude to validate but why doesn't the 'echo t' work?
Why check for a 'submit' post variable when you're working with latitude?
if(isset($_POST['latitude']))
{
echo "t";
$latitude=$_POST['latitude'];
}
else
{
echo "Please insert a latitude . <br>";
$error = $error + 1;
}
Also there is no need for a name on the submit button
<input type="submit" value="Submit">
You just have your form rendering messed up, this works:
echo "<h1> Seismic Recording </h1>";
echo "<div>";
echo "<form action='' method='POST'>";
echo "Latitude: <input type='text' name='latitude'>";
echo "Longitude: <input type='text' name='longitude'd>";
echo 'Wave Type: <select id="wave_type" name="wave_type">';
echo '<option selected value="s">S</option>';
echo '<option value="p">P</option>';
echo '<option value="surface_wave">Surface Wave</option>';
echo "</select>";
echo "Wave Value: <input type='text' name='wave_value' d>";
echo 'Upload CSV File: <input type="file" name="file">';
echo "<input type='submit' name='submit' value='submit'>";
echo "</form>";
echo "</div>";
if(isset($_POST)){
if(isset($_POST['submit'])){
echo "Submit is alive!!";
}
}
In order to avoid this kind of issues, remember that you can just write plain html, on this way:
<?php
//Mi php code blablabla
?>
/* My html code */
<h1>Wonderful day, isn't it?<h1>
<?php
//Another php tasks and etc
?>
<!--form code start-->
<?php
if(isset($_REQUEST['req']))
{
echo "Please enter any input";
}
?>
<form action="validate2.php" method="POST">
Latitude: <input type='text' name='latitude'>
<input type='submit' name='submit' value='submit'>
</form>
<!--validate2.php code start-->
<?php
if (isset($_POST['submit']))
{
$latitude = $_POST['latitude'];
if ($latitude == NULL)
{
header("Location:frm.php?req=error");
}
else
{
echo "Your input Text is: ".$latitude;
}
}
?>
<!--Second Method-->
<!--form code start-->
<form action="validate2.php" method="POST">
Latitude: <input type='text' name='latitude'>
<input type='submit' name='submit' value='submit'>
</form>
<!--validate2.php code start-->
<?php
if (isset($_POST['submit']))
{
$latitude = $_POST['latitude'];
if ($latitude == NULL)
{
echo '<script>alert("Please enter any input");
window.location.href="frm.php";</script>';
}
else
{
echo "Your input Text is: ".$latitude;
}
}
?>
Try this:-
$curl_connection =
curl_init('http://www.domainname.com/target_url.php');
link:-http://www.html-form-guide.com/php-form/php-form-submit.html

Usage of session to retain to text box value in PHP

Hi I have this and works fine except the following issue.
echo "<form method='post' action=''>";
echo " <input type='text' name='txt1' id='txt1' value=".$_SESSION['txt1'].">";
echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
echo "</form>";
<A submit button here to send the form data>
The issue is if I have a value in the text box like John when I submit the form I retain the original user typed word Jonn in the text box. However if the user type John Carter, when the form is submitted it retains only John. In other words texts only up to first space between the texts. How do I retain entire text?
EDIT
<?php
if(isset($_POST['sendone']))
{
echo "Hello";
echo "<form method='post' action=''>";
echo " <input type='text' name='txt1' id='txt1'>";
echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
echo "</form>";
}
if(isset($_POST['sendtwo']))
{
if($_POST['txt1']=='')
{
echo "Hello";
echo "<form method='post' action=''>";
echo " <input type='text' name='txt1' id='txt1'>";
echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
echo "</form>";
echo "Empty";
}
else
{
$_SESSION['txt1'] = $_POST['txt1'];
echo "Hello";
echo "<form method='post' action=''>";
echo " <input type='text' name='txt1' id='txt1' value=".$_SESSION['txt1'].">";
echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
echo "</form>";
echo "Hit success!";
}
}
?>
You need to include the single quotes around the value for the value= attribue when echoing the _SESSION["txt1"], otherwise the resulting HTML isn't valid .. like this:
<?php
if(isset($_POST['sendone']))
{
echo "Hello";
echo "<form method='post' action=''>";
echo " <input type='text' name='txt1' id='txt1'>";
echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
echo "</form>";
}
if(isset($_POST['sendtwo']))
{
if($_POST['txt1']=='')
{
echo "Hello";
echo "<form method='post' action=''>";
echo " <input type='text' name='txt1' id='txt1'>";
echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
echo "</form>";
echo "Empty";
}
else
{
$_SESSION['txt1'] = $_POST['txt1'];
echo "Hello";
echo "<form method='post' action=''>";
// check out this line here:
echo " <input type='text' name='txt1' id='txt1' value='" . htmlspecialchars($_SESSION['txt1']) . "'>";
echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
echo "</form>";
echo "Hit success!";
}
}
?>
Though, it's worth nothing that it is much more performant (and readable!) to use plain HTML where possible, and just use echo for the variables you wish to include.
Check this code.
<form name="" method="post">
<input type="text" name="txt1">
<input type="submit" name="sendtwo">
<input type="submit" name="sendone">
</form>
<?php
if(isset($_POST['sendone']))
{
echo "Hello";
echo "<form method='post' action=''>";
echo " <input type='text' name='txt1' id='txt1'>";
echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
echo "</form>";
}
if(isset($_POST['sendtwo']))
{
if($_POST['txt1']=='')
{
echo "Hello";
echo "<form method='post' action=''>";
echo " <input type='text' name='txt1' id='txt1'>";
echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
echo "</form>";
echo "Empty";
}
else
{
$_SESSION['txt1'] = $_POST['txt1'];
echo "Hello";
echo "<form method='post' action=''>";
// check out this line here:
echo " <input type='text' name='txt1' id='txt1' value='" . $_SESSION['txt1'] . "'>";
echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
echo "</form>";
echo "Hit success!";
}
}
?>

Categories