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
Related
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>";
}
}
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"];
}
?>
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>  ";
echo "<input type='submit' name='acc' value='Accept'>   <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>  ";
echo "<input type='submit' name='acc' value='Accept'>   <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
I have this and works fine. But can someone tell me is there a SMARTER way to validate this?
In the validation here, you may find that the echo "<form....... is replicated thrice to make sure that the form content is also visible along side the validation message when the form is submitted. Im sure there is a more refined method of validating text box content without form being replicated multiple times.
Also appreciate if some one can suggest me a method to retain the text box value after the form is being submitted. Usually when the form is submitted the value you enter disappears.
Thanks
<?php
include ("../connection/index.php");
?>
<form method='post' action=''>
<input type="submit" name="sendone" id="sendone" value="OneClick">
</form>
<?php
if(isset($_POST['sendone']))
{
echo "Hello";
echo "<form method='post' action=''><input type='text' name='txt1' id='txt1'><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></form>";
}
if(isset($_POST['sendtwo']))
{if($_POST['txt1']=='')
{
echo "Hello";
echo "<form method='post' action=''><input type='text' name='txt1' id='txt1'><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></form>";
echo "Empty";}
else
{
echo "Hello";
echo "<form method='post' action=''><input type='text' name='txt1' id='txt1'><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></form>";
echo "Hit success!";
}}
?>
EDIT
Concept
<hard coded submit1 button>
<dynamically create a text box with a submit2 button>
<when submit2 is activated is should validate the text box content>
When the validation happens both the vaidated message and the text box should be visible
did you mean something like this?
<?php
if(isset($_POST['sendtwo']))
{
echo "Hello";
}
?>
<form method='post' action=''>
<?php
if (!isset($_POST['sendone'])) {
?>
<input type="submit" name="sendone" id="sendone" value="OneClick">
<?
} else {
?>
<input type="hidden" name="sendone" id="sendone" value="OneClick">
<input type='text' name='txt1' id='txt1'<?=isset($_POST['txt1']) && trim($_POST['txt1']) !== '' ? ' value="'.trim($_POST['txt1']).'"' : ''?>>
<input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>
<?
}
?>
</form>
<?php
if (isset($_POST['sendtwo'])) {
if (trim($_POST['txt1']) == '') {
echo 'Empty';
} else {
echo 'Hit success!';
}
}
I have a question, I have a page and a form that I direct it to itself to edit data to database.
I need a way to use the form and redirect itself with action=editcinemas.php?cinema_id=THE ID OF CINEMA
<html>
<body>
<form name="form3" method="post" action="editcinemas.php"> //MY PROBLEM IS HERE I GUESS
<td>
<table>
<?php
require('../classes/cinema_class.php');
$cinema=new cinema($_GET["cinema_id"]);
$cinemas = $cinema->get_all_cinemas();
foreach ($cinemas as $movie)
{
$cinema_id = $movie['cinema_id'];
$cinema_name = $movie['cinema_name'];
$cinema_location = $movie['cinema_location'];
}
echo "<input name='cinemaid' type='hidden' id='cinemaid' value=" . $cinema_id . '><br/>';
echo "Cinema Name :";
echo "<input name='cinemaname' type='text' id='cinemaname' value=" . $cinema_name . '><br/>';
echo "Cinema Location :";
echo "<input name='cinemalocation' type='text' id='cinemalocation' value=" . $cinema_location . '><br/>';
?>
</table>
<input type="submit" name="Edit" value="Edit"> <input type="reset" name="Reset" value="Reset"/><INPUT Type="button" VALUE="Back" onClick="history.go(-1);return true;">
</form>
</body></html>
<?php
$cinemaid=$_POST['cinemaid'];
$cinemaname=$_POST['cinemaname'];
$cinemalocation=$_POST['cinemalocation'];
if ($_POST) {
if(empty($cinemaname)){
echo "Cinema Name is a must"; }
else{
$update_movie=mysql_query("UPDATE `memoire`.`cinema`
SET `cinema_name`= '$cinemaname', `cinema_location`= '$cinemalocation'
WHERE `cinema_id`='$cinemaid'");
if($update_movie)
{ header("Refresh: 0;url=../listallcinemas.php");}
else
{ echo "error in registration".mysql_error(); }
}
?>
Just simply append it, like this:
<form name="form3" method="post" action="editcinemas.php?cinema_id=<?php echo $_GET["cinema_id"]; ?>">