Hi I have a simple form which sends data to process.php.
My problem is how do I clear the select option after submitting the form?
I have a jQuery script that clears the select and brings back its default value but the problem is the default value is the one being passed in my process.php file instead of the selected value.
Here is my jQuery code and my form:
jQuery(document).ready(function() {
jQuery('#custom-submit-input').click(function(){
jQuery('#form-option').prop('selectedIndex',0);
});
});
echo "<form action='file-here/process.php' method='post' enctype='multipart/form-data' target='upload_target' id='form-reset'>";
echo "<iframe id='upload_target' name='upload_target' ></iframe>";
echo "<select name='id' id='form-option' class='test-only'>";
echo '<option selected="selected">' .'Choose a User'. '</option>';
foreach ($registeredUsers as $key => $value) {
$registered = JFactory::getUser($value);
echo '<option value="'.$registered->id.'">'.$registered->name.'</option>';
}
echo "</select>";
echo "<input name='uploadedfile' type='file' id='custom-file-input' class='test-only' /><br/>";
echo '<input type="submit" name="submit" value="Upload" id="custom-submit-input" >';
echo "</form>";
You can use either
jQuery('#form-option').val(''); // sets the value of the select to the first option with a value of ''
Or
jQuery('#form-option').get(0).selectedIndex = 0; // sets the value of the select back to the first option
you can use .reset() function.
jQuery(document).ready(function() {
jQuery('#custom-submit-input').click(function(){
jQuery('#form-option').reset();
});
});
Related
I'm learning PHP and I need help to "send" informations on an another page.
I read a text file and I put data on 2 arrays :
$nomVille;
$nomFichier;
$index; // index is use for arrays
Now I want to build a drop-down list and a submit button.
The drop-down list : show all items on $nomVille
The button : open "villes.php"
villes.php will have to retrieve $nomFichier and $index but I'm not able to do that.
Here is a part of my code :
echo 'Make your choice : ';
echo "<select name='ville'>";
foreach($nomVille as $option){
echo "<option value='{$option}'>{$option}</option>";
}
echo "</select>";
echo '<FORM METHOD="post" ACTION="villes.php">';
echo '<INPUT TYPE="submit" VALUE="Display">';
echo '</FORM>';
Is that correct ?
You've not got your select element wrapped in form tags which will prevent it being sent. What you actually want is this:
echo '<FORM METHOD="post" ACTION="villes.php">';
echo 'Make your choice : ';
echo "<select name='ville'>";
foreach($nomVille as $option){
echo "<option value='{$option}'>{$option}</option>";
}
echo "</select>";
echo '<INPUT TYPE="submit" VALUE="Display">';
echo '</FORM>';
On the page "villes.php", you should then be able to refer to the variable $_POST["ville"] which will contain the selected option in the select.
echo $_POST["ville"];
In order to transfer the values of $nomFichier and $index, you want to send them as hidden elements as part of the form, so add the lines:
echo "<input type='hidden' name='nomFichier' value='".$nomFichier."'>";
echo "<input type='hidden' name='index' value='".$index."'>";
Which will again, be able to be grabbed from the $_POST array.
Moving your select into the FORM element will allow the application to post the data on submit.
echo 'Make your choice : ';
echo '<FORM METHOD="post" ACTION="villes.php">';
echo "<select name='ville'>";
foreach($nomVille as $option){
echo "<option value='{$option}'>{$option}</option>";
}
echo "</select>";
echo '<INPUT TYPE="submit" VALUE="Display">';
echo '</FORM>';
From the villes.php page you then have data retrievable in the data that you can access like this:
if (isset($_POST['ville'])) {
$someVar = $_POST['ville'];
}
You'll find it worth your time to Google "PHP form tutorial" and seeing how things work from there.
<?php
require "../db/dbconfig.php";
$gal=mysql_query("select * from gallery");
$numrows=mysql_num_rows($gal);
if($numrows>=1)
{
echo "<form action='delete.php' method='post' name='f2' id='f2'>";
echo '<table id="rqst" style="display:block;" border="0" cellpadding="12" cellspacing="3" width="500px">';
echo "<tr><th>Tick to select</th><th>Images in Gallery</th></tr>";
while($row=mysql_fetch_array($gal)
{
$imgfile=$row['ImgName'];
$Image="<img src=gallery/".$imgfile." width='230px' height='150px'/>";
$img_name=$imgfile;
echo "<tr>";
echo "<td><input type='checkbox' name='imgs[]' value='$img_name'></td><td>".$Image."</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td colspan='3' align='right'>";
echo "<input type='submit' value='Delete' name='del'></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
?>
This is my code....This will display images from gallery and checkboxes associated with them. When I click delete button with unchecked checkboxes an alert should come like this "Please check at least one checkbox"..How to do that??
My next problem is,,when I click delete button after checked checkbox, alert should come like this=" Do you want to delete?? "...If clicked Ok,the image must be deleted else do nothing...Please help ...Thanks in advance....
check this below link for validation using jquery:
http://jsfiddle.net/susheel61/U3Unk/2/
<form id="form">
First name: <input type="checkbox" name="firstname" class="check"><br>
<button>test</button>
</form>
$("button").on("click",function(e){
var status = $(".check").is(":checked");
e.preventDefault();
if(!status){
alert("this is not checked");
}
});
Yes, you can do it by javascript or jquery to validate whether your atleast one checkbox is select or not. So, for that you need to give a common class for all checkbox as example
<input type="checkbox" name="firstname" class="addchk">
Now in your submit button call a javascript function which validate the matter.
<input type='button' value='Delete' name='del' onclick='delete_checked()' />
Now write a function to validate whether any checkbox is selected.
function delete_checked()
{
var status = $(".addchk").is(":checked");
e.preventDefault();
if(!status){
alert("this is not checked");
}
else
{
// SUbmit yout form
}
}
I am trying to submit a form (or a request) to do some tasks. The form doesn't have input field except a submit button.
I know it's kinda strange but I want to submit the form and get the data by using only php.
Is that possible?
My codes
if($_GET['submitImage']){
//do the stff I want......
}
<form id='formSubmitImage' name='submitImage' method='get' action='cm_ct_generate_preview.html'>
<?php
echo "<table>";
echo "<th>ID</th>";
echo "<th>Type</th>";
echo "<th>Name</th>";
echo "<th>Image</th>";
foreach($tests as $test){
echo '<tr>';
echo '<td>';
echo $test['ID'];
echo '</td>';
echo '<td>';
echo $test['Type'];
echo '</td>';
echo '<td>';
echo $test['Name'];
echo '</td>';
echo '<td>';
echo $test['FileName'];
echo '</td>';
echo '</tr>';
}
echo "</table>";
echo "<input type='submit' value='Set Images'></input><br>";
?>
</form>
Are there any alternative way to do this? Thanks a lot!
Instead of:
echo "<input type='submit' value='Set Images'></input><br>";
Why not:
echo "<button type='submit'>Set Images</button><br>";
You can add a GET parameter to the end of your action URL (in your case it would then be something like cm_ct_generate_preview.html?submitImage=1 or you can add a hidden input field in the form and check if that is submitted, for example:
<input type="hidden" name="submitImage" value="1" />
As your form method is already set to GET it should be sent as a GET parameter.
If the user needs to activate it, let them just press the submit button. However if you want to do it automatically, you will need to use a little javascript. To send the data with it, you can just specify your inputs like this:
echo '<input type="hidden" name="id" value="'.$test['ID'].'">';
the input fields will not appear but the data will be send like with an input field.
I want to refresh the same page and display the entered value in text box on the same page after clicking the link or button.
I have the following code:
<?php
echo '<h3>Fee Payment</h3>';
echo "<form id='myFormId' name='myFormName'>";
echo " <input type='text' name='myTextField'>";
echo "<a href='{$_SERVER['PHP_SELF']}?student_no=myTextField'> Search Student</a>";
if (!(isset($_GET[student_no])))
{
echo "No Student is available.";
}
else
{
$student_no = $_GET['student_no'];
echo "Student NO:".$studen_no;
}
?>
Please guide me how to achieve the goal and whats the error my code.
<?php
echo '<h3>Fee Payment</h3>';
if(isset($_POST['myTextField']))
$value=$_POST['myTextField'];
else
$value='';
echo "<form id='myFormId' name='myFormName' method='post'>";
echo " <input type='text' name='myTextField' value='$value'>";
echo "<a href='{$_SERVER['PHP_SELF']}?student_no=myTextField'> Search Student</a>";
You also need a submit button in the form, and a form close tag.
What you want is AJAX. I am just providing you an example of how you can proceed by using jQuery, but you can use any other library / framework or any other way also. You can check this article also for more usability details.
In the main page:-
<?php
echo '<h3>Fee Payment</h3>';
echo "<form id='myFormId' name='myFormName'>";
echo "<input type='text' name='myTextField' id='myTextField' />";
echo 'Search Student';
echo "</form>";
?>
<script type="text/javascript">
$('#inline_submit_a').click(function(evt){
$.ajax({
type: "GET",
url: "handler.php",
data: {text:$('#myTextField').val()}
});
evt.preventDefault();
return false;
});
</script>
In the "handler.php" page:-
<?php
if (!(isset($_GET[student_no])))
{
echo "No Student is available.";
}
else
{
$student_no = $_GET['student_no'];
echo "Student NO:".$student_no;
}
?>
You can write all the logic related to the database in the "handler.php" page.
Hope it helps.
You're using a link to submit the form, but you should be using a submit button.
<input type="submit" value="Search student" />
You didn't close the <form> tag.
You're using $_GET[student_no] which will make PHP look for a definition of student_no. Since it's a string, express it as one. $_GET['student_no'] is better.
Why can't you submit the form? Do you want to have a link for searching instead of a button?
You could configure the link to submit the form via javascript and change the form action to GET like this:
<?php
echo '<h3>Fee Payment</h3>';
echo "<form id='myFormId' name='myFormName'
action='{$_SERVER['PHP_SELF']}' method='GET'>";
echo " <input type='text' name='student_no'>";
echo "<a href='javscript:document.forms.myFormName.submit()'>
Search Student</a>";
...
I have this form with multiple checkboxes with same name and all of checkboxes are read from a database:
echo "<FORM action='check.php' method=POST>"; ?>
<div style='color:#414141; font-size:15px;'>
<?php
while($rows=mysql_fetch_array($result)){
?>
<input name="check[]" type="checkbox" id="checkk[]" value="<? echo $rows['ID']; ?>"><? echo $rows['checkboxname']; ?>
<BR>
<?php
}
echo "</div>";
echo "<input name='' type='submit' id='get' value='Next'>";
echo "</FORM>";
I need a form validation to the next file.When I click on Submit Button(Next),show me the next page only if I checked a checkbox.I preffer a javascript validation method.
You can add a click event listener for the submit button and check if atleast one checkbox is checked and submit if true.
Something like below should work,
Try below,
echo "<input name='' type='submit' id='get' value='Next' onclick='return validateCheckbox()'>";
<script>
function validateCheckbox () {
var checkboxes = document.getElementsByName('check[]'); //selected by name since OP wanted to select by name
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].checked) {
return true; // found 1 checked checkbox
}
}
return false; //none checked, so cancel submit
}
</script>