Textbox empty after submitting - javascript

How can I keep the value of textboxes after submitting a form?
When I press the submit button the values dissapear.
What I want is that the values don't dissapear from the textbox after pressing submit button.

in PHP you can do
<input type="text" name="fieldName" value="<?php echo isset($_POST['fieldName']) ? $_POST['fieldName'] : '' ?>" />

since HTTP is a stateless protocol so it lost previous data from the submitted form.
You can use session or you can do it like this:
<input name="email" value="<?php isset($_REQUEST['email']) ? $_REQUEST['email'] : '' ?>">
it will check if value exists in the super global $_REQUEST if exists then echoing it as value of the input field. set like this for all input field.

Related

How to get and display input text from one page to another page

I have input field with some text in the page
http://localhost/example/package-details/?v=package1/
How can i display that value in other page?
http://localhost/example/book-now/?v=package1/
a rough solution would be,,
on firstpage.php
<form action="secondpage.php" method="post">
<input type="text" name="textinput">
<other inputs>
<input type ="submit" value="submit">
</form>
On secondpage.php, get values of input fields using $_POST['textinput'] and store it into session variable so now you can access it on any page
on secondpage.php
<?php
// Start the session
session_start();
// Set session variables
$_SESSION["textinput"] = $_POST['textinput'];
//access this session variable
echo "value of textinput is " . $_SESSION["textinput"];
?>
also check out this link to know more about php $_SESSION

Check for empty field for my username and password

<?php
if ( isset( $_GET['fail']) && !empty($_POST["uname"]))
{
echo '<script language="javascript">';
echo 'alert("Your name or password is incorrect")';
echo '</script>';
}
How do I check for empty field when I press the submit button?
Why do you have a "GET" and a "POST" check in your php code? You can't use both verbs when sending an HTTP request unless your form in html looks something like:
<form id="myForm" method="post" action="myAction.php?fail=someStatus">
<input type="text" name="uname" required />
</form>
If you are not sending the value of fail, the isset( $_GET['fail']) will always return false and your code will never get past your if
<?php
// isset( $_GET['fail']) is always false
if ( isset( $_GET['fail']) && !empty($_POST["uname"]))
{
echo '<script language="javascript">';
echo 'alert("Your name or password is incorrect")';
echo '</script>';
}
You could use required attribute of html5 in your input tag as
<input type="text" name="USERNAME" required>
<input type="text" name="PASSWORD" required>
This won't allow to submit form without content in the textbox.
I don't see what you are trying to do here. If you submit your form with Ajax you can request a php script (eg. form_submit.php) where you validate your form fields and return the response which will be used by your javascript to display something if there is an error (or not).
Here is simple process to explain how you can do:
html form submitted => Javascript with Ajax call to form_submit.php => Form verification with PHP => Return result from verification => Javascript Ajax get the response and do what you want to do like displaying an error/success popin for example.

Cant submit form

Basically I've got a form with 5 radio buttons. No submit button. I want the form to run when the user clicks on any of the radio buttons, so this is what I did.
<input id="5" type="radio" name="star" onchange="this.form.submit();" <?php if ($row["star"] =="5") echo "checked";?> value="5"/>
a querystring is required for the form so I'm using a form action like this
<form name="submit" action="http://example.com/page.php?id=<?php echo $urlid;?>&title=<?php echo $title;?>" method="POST">
and my php is
if ($_POST["submit"]) {
$rating = $_POST['star'];
$title = $_GET['title'];
$verification = ($_GET['verification']);
} else {
//run the page like usual
}
After testing, I found that onclick, it runs the form action, but on the php side, it goes to "else" where is runs the page like usual instead. Any ideas?
Your PHP is checking if $_POST['submit'] contains a value. Your form does not contain a form element with the attribute name="submit", so therefore it fails and moves straight to the else statement.
If you want to check if the form was posted then you should instead check for:
if (!empty($_POST)) {}
or
if ($_SERVER['REQUEST_METHOD'] == 'POST') {}
The form element seems to have invalid attributes, missing a quote and space.
It's generally easier to write a little more code, and keep it clearer
<?php
$url = "http://example.com/page.php?id=". $urlid ."&title=". $title;
?>
<form name="submit" action="<?php echo $url; ?>" method="POST">
Since you are checking with -
if ($_POST["submit"]) { // this checks whether there is any item named 'submit' inside the POST or not
} else {
//run the page like usual
}
The easiest would be to put a hidden item with name submit so that the check validates to true-
<form .. >
....
<input type='hidden' name='submit' value='submit' />
</form>

Dropdown list triggering required field validation when disabled

I have this function here, when I disable the report field from the code below, I can't submit the form because the required field triggers, even though there is a selected value in the dropdown. When i remove the disabling of the list. I was able to submit the form:
window.onload = function()
{
var x = document.getElementById("jform_report");
x.disabled = true;
}
jQuery('#jform_report').prop('disabled', true).trigger("chosen:updated");
What might be triggering the required field? I tried displaying the dropdown's value by using a alert box, It shows the correct selected value so I'm wondering why is the required field validation triggering.
here is the HTML code :
<input type="hidden" name="jform[report]" value="<?php echo $this->item->report; ?>" />
<div class="control-group">
<div class="control-label"><?php echo $this->form->getLabel('report'); ?></div>
<div class="controls"><?php echo $this->form->getInput('report'); ?></div>
</div>
When I click submit, it points to this line of code in the inspect element:
<a class="chzn-single" tabindex="-1"><span>Java</span><div><b></b></div></a>
does it have something to do with tabindex?
When you Disable your control, it suggests that the field is not selected, therefore if it is a required field and you have to disable it, make sure you put in a hidden field (with the same name as the required field) with the selected value. It will pass your validation

Send value with Onchange and javascript function

I'm new at JavaScript and I need some help to submit my form when anyone select anything from the dropdown list. Here is the sample code :
<form action="" method="post">
<select name="car" onChange="this.form.submit()">
<option value="car1">car1</option>
<option value="car1">car1</option>
<option value="car1">car1</option>
</select>
<!-- Some other code -->
<input type="submit" name="sentvalue">
</form>
This is my form and when anyone selects the dropdown, the form automatic submit but I also need the sentvalue when automatic submit the form. So can anyone help me to capture the sentvalue when anyone select the dropdown.
If you want to send any other data with the form submit you can send it in hidden inputs
<input type='hidden' name='data1' value='Test1' />
<input type='hidden' name='data2' value='Test2' />
//etc
And if you want the value of submit button with the form just set the value attribute into your code for submit button every this else seems fine,
<input type="submit" name="sentvalue" value="Test1" />
Hope this answers your question
Just give the <input> tag a name
<input name="submitbtn" type="submit" value="sentvalue" />
then you can get it by $_POST['submitbtn'] in case of PHP which value is "sentValue"
It sounds like you want the value of the input field (sentvalue) to be submitted as well. But how do you guarantee that the value was specified by the user? Is this supposed to be a hidden input field?
Either way your input tag is incomplete. This sounds better:
<input type="text" name="sentvalue"></input>
Also, when you submit, the value of this field (sentvalue) will be passed in too. As long as your tags are right you don't need to worry.
You will have to create a hidden input element
<input type='hidden' id='sentvalue' value='' />
and call a function on submit instead of directly submitting
Use this in the function to set the values and submit
var e = document.getElementByName("car");
document.getElementById("sentvalue").value = e.options[e.selectedIndex].value;
this.form.submit();

Categories