So i have a form method post in index.php where in it will send the data from a textbox to another page which is print.php.
now what i want to do is if the textbox from index.php is null it wont redirect to print.php or if it redirect to print.php it will be redirected back to index.php.
index.php format
<form action="print.php" method="post" target="_blank">
<input type="text" name="faidf" id="faidf" size="25" value="" maxlength="25"/></td>
<input type ="submit" value="Print">
print.php
<?php
$faidf = $_POST['faidf'];
if(isset($_POST['faidf'])) {
echo "<td><font size=2>FAID:$faidf</td><td></font></td>";
}
else {
echo "FAID is missing";
}
?>
instead of FAID is missing could i redirect it home because i have about 10more php wherein it needs the variable of $faidf so the whole printd.php is utterly useless if the textbox is blank.thanks
You can do this by two ways:
Way 1:
Use JQuery/JavaScript for the form validation process onsubmit of the form. It will redirect only in case where there is data found in textbox.
Way 2:
Check the length of provided post data and if the length is less than 1 return it to the previous page.
I will advice you to use the JavaScript/JQuery as it will run on all cross browsers and easy to implementation and changed easyly
first add onchange function to your textbox
<input type="text" onchange="myFunction(this)" name="faidf" id="faidf" size="25" value="" maxlength="25"/>
and add an Id for the button
<input type ="submit" value="Print" id="btn" />
and add script tag in your page :
function myFunction(e)
{
var x=document.getElementById("btn");
if (e.value == ''){
x.setAttribute('disabled','disabled');
}else{
x.removeAttribute('disabled');
}
}
and instead of your echo at print.php add location header
header("Location: index.php");
Related
I have a question from php and I'm not expert in php.
1.I have html page with one form include a text box and submit button .
2.I have a static target url like this : https://example.com/invoice/XXXXXXXXXXXXXX
XXXXXXXXXXXXXX is just numbers and has 14 characters.
*** What I need is that my customer enter its 14 characters number in input text form and when it submit , goes to target url.I want to check input form for entry numbers too.
I make a sample form like this but not work:
<form action="https://example.com/invoice" class="pey-form" method="get">
<input type="text" id="peyid" name="peyid" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '$1');" maxlength="14" ><br><br>
<input type="submit" value="submit">
</form>
What can I do?
As hassan said , you can do this only with javascript.
This will redirect to the url what you desired.
document.querySelector("form").onsubmit = function(){
this.setAttribute("action",this.getAttribute("action")+"/"+document.querySelector("[name=peyid]").value);
}
For example
If document.querySelector("[name=peyid]").value = 12345678901234 The url will look like https://example.com/invoice/12345678901234?peyid=12345678901234
So if you just need to redirect to that url you don't even need form just
<input type="text" id="peyid" name="peyid" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '$1');" maxlength="14" ><br><br>
<input type="button" value="submit">
<script>
document.querySelector("[type=button]").onclick = function(){
location.href = `https://example.com/invoice/${document.querySelector("[name=peyid]").value}`;
}
</script>
Using PHP—to receive a form value, validate it, apply it to a URL, and redirect the client to that location, use $_POST, preg_match(), string interpolation, and header().
/invoice/index.php:
<?php
if ( isset($_POST['peyid']) && preg_match("/^\d{14}$/", $_POST['peyid']) ) {
header("Location: http://www.example.com/invoice/{$_POST['peyid']}");
exit;
}
?>
<html><head><title>oops</title></head><body>An error occurred.</body></html>
my page receives data which i retrieve with $_post. I display some data and at the bottom of page my button has to save data to mysql. I could submit form to next page, but how do i access the data that I have retrieved with post then? Lets say i have following code (in reality alot more variables ..):
<?php
$v= $_POST["something"];
echo $v;
echo "Is the following information correct? //this would be at the bottom of the page with the buttons
?>
<input type="button" value="submit data" name="addtosql">
You can do it in two methods:
1) You can save the POST variable in a hidden field.
<input type="hidden" name="somevalue" value="<?php if(isset($_POST["something"])) echo $_POST["something"];?>" >
The hidden value also will get passed to the action page on FORM submission. In that page you can access this value using
echo $_POST['somevalue'];
2) Use SESSION
You can store the value in SESSION and can access in any other page.
$v= $_POST["something"];
session_start();
$_SESSION['somevalue']=$v;
and in next page access SESSION variable using,
session_start();
if(isset($_SESSION['somevalue']))
echo $_SESSION['somevalue'];
Take a look. Below every thing should be on single php page
// first create a function
function getValue($key){
if(isset($_POST[$key]))
return $_POST[$key];
else
return "";
}
// process your form here
if(isset($_POST['first_name']){
// do your sql stuff here.
}
// now in html
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="first_name" value="<?php echo getValue("first_name"); ?>" />
<input type="submit" />
</form>
I am learning PHP and I have a page that reloads back to itself. I want to know if you can ignore a certain function on the initial loading of the page and only call it once the form submit button has been clicked.
The page is passed a 'ticketID' and loads the information from it. I then want to be able to add a note using the following form method:
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<strong>Add Note:</strong>
<textarea name="note" rows="5" cols="40" value=><?php echo htmlspecialchars($note);?></textarea>
<span class="error">*<?php echo $noteErr;?></span><br>
The user then clicks on a submit button to submit the note for processing:
<button type='submit' name='ticketID' value= <?php echo $_POST['ticketID'];?> >View</button>
</form>
The 'ticketID' is then passed back to the page to reload the information.
If the submit button is pressed and no note has been entered I want a message box to display informing the user to include a note. I have tried:
if (!empty($_POST["note"]))
{
echo "This has updated...";
} else {
echo "Missing!";
}
However this loads the error message even on the initial load of the page. I have tried setting a variable to the POST ticketID value and clearing the POST value after the page has displayed and before testing for the error message:
$tempTicketID = $_POST['ticketID'];
$_POST['ticketID'] = NULL;
Then testing the error message, and finally setting the POS value back before the page ends to allow it to reload correctly again:
$_POST['ticketID'] = $tempTicketID;
However the POST value doesn't save and the page reloads with no information.
Any help would be great appreciated.
Here's the full code layout:
##LOAD THE PAGE INFO...
#Set the temp variable and clear the post value
$tempTicketID = $_POST['ticketID'];
$_POST['ticketID'] = NULL;
#Load the form
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<strong>Add Note:</strong>
<textarea name="note" rows="5" cols="40" value=><?php echo htmlspecialchars($note);?></textarea>
<span class="error">*<?php echo $noteErr;?></span><br>
<button type='submit' name='ticketID' value= <?php echo $_POST['ticketID'];?> >View</button>
</form>
#Test if the note is empty and the form button has been pressed
if (!empty($_POST["note"]))
{
echo "This has updated...";
} elseif (empty($_POST["ticketID"] {
echo "Missing!";
}
#Set POST value back to reload the page
$_POST['ticketID'] = $tempTicketID;
We need to restructure the form just a bit to make this happen. You can check if the form is submitted by testing for the button that must be clicked to submit. However, you're using that button for multiple purposes. To simplify, we'll have a separate submit button, and pass the ticketID value through the form with a hidden input. You shouldn't need the code that unsets the $_POST values.
<button type='submit' name='submit'> View</button>
<input type='hidden' name='ticketID' value= <?php echo $_POST['ticketID'];?> />
Then you can test if the form has been submitted with this quick check:
if (isset($_POST['submit'])) {
if (!empty($_POST["note"]))
{
echo "This has updated...";
} else {
echo "Missing!";
}
}
I have a form and the form method is set to post. It is connected with a second file which creates a session.
I would like to create a page with a unique url from that second file automatically with PHP and redirect to that page.
If possible insert some code to the created page.
This is the form code:
<form action="file_upload.php" method="post"
enctype="multipart/form-data">
Title: <input type="text" name="file_title" maxlength="55" required><br>
Description: <input type="text" name="file_description" maxlength="80" required><br>
<input type="submit" value="Title Magic">
</form>
This is the 2nd file code:
$title = substr($_POST['file_title'],0,55);
$description = substr($_POST['file_description'],0,80);
if(isset($_POST['file_title']))
$_SESSION['ses_file_title'] = $_POST['file_title'];
if(isset($_POST['file_description']))
$_SESSION['ses_file_description'] = $_POST['file_description'];
What I would like it to do is redirect to a page with a unique url and echo out these statements.
you have to get a value in hidden field after that forward it in $abc variable with if(isset()) condition,
after that for redirection use<script>window.location="filename.php?edt=<?php echo $abc ?>"</script> you'll get id with session on next page and use MySql to retrieve data for automatically created page... hope this will help
You can add yourcode in hidden field or use file_upload.php?code=yourcode and you can access it by request and use below php code for redirection.
header('Location: unique url ');
I have a PHP form that has some drop down selections and text field entries. If the user selects the wrong item from the dropdown, when they submit the form, I have it so that it will show an error message to the user and force the browser to go back to the previous page. The problem is that the user has to re-enter all of the information.
How do I make the form save the data until the form submit is successful?
EDIT:
Form submit method is $_POST and the form is being submitted to another page.
This would have to be done with strictly PHP as Javascript/Jquery solutions can be script blocked by more secure users.
Here you go. This will work, and is not dependent on Javascript:
form.php //the form page
<?php session_start(); ?>
<form method="post" action="action.php">
<input type="text" id="input1" value="<?php echo (isset($_SESSION['fields']) ? $_SESSION['fields']['input1'] : '') ?>" />
<input type="text" id="input2" value="<?php echo (isset($_SESSION['fields']) ? $_SESSION['fields']['input2'] : '') ?>" />
</form>
action.php //the action page
<?php
session_start();
//do your validation here. If validation fails:
$_SESSION['fields']['input1'] = $_POST['input1'];
$_SESSION['fields']['input2'] = $_POST['input2'];
//redirect back to form.php
?>
Is the form a POST or a GET? Either way, you have access to all the submitted fields in the PHP variables $_POST or $_GET. Within your HTML you can pass those values (if set), to the default value of each HTML input element. This way, if it is a first time, they will be blank, if there was an error, the values will repopulate.
If they're select values, you can do something like this:
<select name="my_select" id="my_select">
<option value="123"<?php if($_REQUEST['my_select'] == 123) echo ' selected="selected"; ?>>123</option>
</select>
If you have regular text inputs, you can simply apply the $_REQUEST variable to the value attribute:
<input type="text" name="my_text" value="<?php echo $_REQUEST['my_text'] ?>" />
I suggest a preventing the page from navigating away from the submission until the data is verified. Enter jQuery :)
<script type="text/javascript" src="jquery-library.js"></script>
<script type="text/javascript">
$(document).ready(function(){
// Wait for the user to click on your button
$('#submit_button').click(function(){
// Check each form field for an appropriate value
if ($('#form_field1').val() != 'something I expect')
{
alert('Wrong submission!');
return false;
}
// Forward the user to some url location
window.location = 'url';
return false;
});
});
</script>