validating dynamic text box content in PHP - php

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!';
}
}

Related

change to php 8 some POST data after HTML form submit is empty [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 days ago.
I pass data from a form (once hidden and once by input) by click to a function. But unfortunately the data on the hidden input field is given to the $_POST
<?php>
$abschlag1 = "gelb";
$ndbw01 = 8;
echo "<form name='Formular' action='' method='post' >";
echo "<td align=center><input type='text' pattern='\d*' maxlength='2' id='L01w' name='L01w' size='2'>" . $ndbw01 . "</td>";
echo "<input type='hidden' id='abschlag1' name='abschlag1' value=$abschlag1>";
echo "<input type='submit' name='submit1' value='Speichern' onclick='chkFormular();'/>";
echo "</form>";
?>
<script type="text/javascript">
function chkFormular() {
<?php
if(isset($_POST['submit1'])) {
$abschlag1 = $_POST['abschlag1'];
$L01w = $_POST['L01w'];
}
?>
}
</script>
The result ist:
$_POST['abschlag1‘] => gelb
$_POST['L01w‘] => empty
I hope, it is now a little clearer.
You can't run a PHP script on a JavaScript event directly. PHP usually is executed at page generation on the server side but if really needed you can run a PHP script on a JavaScript event using Ajax for example.
In order to help you, correct me if I'm wrong, but I think what you want to do is having your fields still completed with the sent values after the form is submitted. You can do it like this using:
value='<?php if isset($_POST["field"]) echo $_POST["field"]; ?>'
<form name='Formular' action='' method='post'>
<td align='center'>
<input type='text' pattern='\d*' maxlength='2' id='L01w' name='L01w' size='2' value='<?php if isset($_POST['L01w']) echo $_POST['L01w']; ?>' /><?= $ndbw01; ?>
</td>
<input type='hidden' id='abschlag1' name='abschlag1' value='<?php if isset($_POST['abschlag1']) echo $_POST['abschlag1']; ?>' />
<input type='submit' name='submit1' value='Speichern' onclick='chkFormular()' />
</form>
If you still want to keep your data in variables, you update your code to this:
<?php
if(isset($_POST['submit1'])) {
$abschlag1 = $_POST['abschlag1'];
$L01w = $_POST['L01w'];
}
?>
<form name='Formular' action='' method='post'>
<td align='center'>
<input type='text' pattern='\d*' maxlength='2' id='L01w' name='L01w' size='2' value='<?= $L01w; ?>' /><?= $ndbw01; ?>
</td>
<input type='hidden' id='abschlag1' name='abschlag1' value='<?= $abschlag1; ?>' />
<input type='submit' name='submit1' value='Speichern' onclick='chkFormular()' />
</form>

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

How to use string from condition1 into condition2?

I have the following code:
<form action='' method='POST' id='form1'>
imdbcode : <input type='text' id='imdbcode' name='imdbcode' /><br/>
<input type='submit' name='submit' value='Get'/>
</form>
<?php
if(isset($_POST['submit'])){
.
.
.
$title = ... ;
echo "
<form action='' method='POST' id='form2'>
<input type='submit' name='Send' value='Send'/>
</form>";
}
if(isset($_POST['Send'])){
//I WANT TO USE $Title from condition1
} ?>
I want to use $title in second condition.
It prints $title in first condition! But doesn't print after closing condition!
How can I do that?
You can't.
Before you can enter the if(isset($_POST['Send']))
you need submit this one:
echo "
<form action='' method='POST' id='form2'>
<input type='submit' name='Send' value='Send'/>
</form>";
but the moment you submit this, the page will be refreshed and $_POST['submit'] will be deleted, without this variable the $title will not exist.
to fix this or make the $title value alive until the next refresh of the page. you must include the $title value along with the form2.
echo "
<form action='' method='POST' id='form2'>
<input type='hidden' name='title' value="$title"/>
<input type='submit' name='Send' value='Send'/>
</form>";
and when the user send that form2 you can access the title by using
$_POST['title']
As two your forms are different - there's no connection between them unless you explicitly provide it.
Simple solution can be just echo your $title as a hidden field in a form:
if(isset($_POST['submit'])){
$title = ... ;
echo "
<form action='' method='POST' id='form2'>
<input type='hidden' name='title' value='" . $title . "'/>
<input type='submit' name='Send' value='Send'/>
</form>";
}
if(isset($_POST['Send'])){
echo $_POST['title'];
// do other stuff
}
Another solution is to use sessions:
if(isset($_POST['submit'])){
$title = ... ;
$_SESSION['title'] = $title;
echo "
<form action='' method='POST' id='form2'>
<input type='submit' name='Send' value='Send'/>
</form>";
}
if(isset($_POST['Send'])){
echo $_SESSION['title'];
// do other stuff
}
In case of using sessions don't forget to use session_start and probably to unset $_SESSION['title'] in the end of second submit.
<form action='' method='POST' id='form2'>
<input type='hidden' name='text' value='<?php echo $title; ?>
<input type='submit' name='Send' value='Send'/>
</form>
Try to add a hidden input before the Send button.

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

how to use two form's post value together

I have a form on a page like:
<form action='search.php' method='POST'>
<input type='text' name='specialist' />
<input type='submit' name='submit' />
</form>
on search page there is another form like
<form action='' method='POST'>
<input type='submit' name='anygender' />
</form>
then i am using
if(isset($_POST['anygender'])){
$speciality = $_POST['speciality'];
echo $speciality;
$a = mysql_query("SELECT * FROM find_doctor WHERE doctor_type LIKE '%$speciality%'");
while ($b = mysql_fetch_array($a)){
echo "<img src='$b[image]' height='150px' width='300px'>"."</br>";
echo $b['name']."</br>";
echo $b['doctor_type']."</br>";
echo $b['location']."</br>";
echo $b['insurance']."</br>";
echo $b['comments']."</br>";
echo $b['address']."</br>";
}
}
then $specialist is showing blank and sql query not working..I want to use both form's post value together.Please tell me how to use first form post value in this. Thanks in advance
Why you need two forms?
<form action='index.php' method='POST'>
<input type='text' name='specialist' />
<input type='submit' name='anygender' />
<input type='submit' name='submit' />
</form>
Maybe you can use one form and check buttom?
Use this code. Check $_POST['submit'] in if condition.
if(isset($_POST['submit'])){
$speciality = $_POST['speciality'];
echo $speciality;
$a = mysql_query("SELECT * FROM find_doctor WHERE doctor_type LIKE '%$speciality%'");
while ($b = mysql_fetch_array($a)){
echo "<img src='$b[image]' height='150px' width='300px'>"."</br>";
echo $b['name']."</br>";
echo $b['doctor_type']."</br>";
echo $b['location']."</br>";
echo $b['insurance']."</br>";
echo $b['comments']."</br>";
echo $b['address']."</br>";
}
}

Categories