I have the code below that gets an id if there is any and asks you to click a button to show you data.
<input type="text" id="date" value="<?php echo $_GET['id']?>" class="css-input" placeholder="Posting Date..." readonly="readonly" /><br /><br />
<input type="button" id="validate" value="Let's get to work!" class="btn" />
The code below takes you to the data automatically without needing to click a button if there is an id there.:
jQuery(document).ready(function() {
jQuery("#validate").click();
});
How can I do an if statement that if #validate is not null then execute the jQuery code else don't. How could I do that whenever I do <?php if(#date != null) I get an error, any ideas?
<?php
if($_GET['id'] != NULL){
?>
<script>
jQuery(document).ready(function()
{
jQuery("#validate").click();
});
</script>
<?php }?>
</head>
Related
I am trying to implement an autosave functionality using ajax and php. Here is my script:
function autosave() {
alert($("#myform").serialize());
$.ajax({
url: "form_creator.php",
data: $("#myform").serialize(),
type: 'POST',
success: function(data){
if(data && data == 'success') {
alert("saved");
// Save successfully completed, no need to do anything
}else{
alert(data);
// Save failed, report the error if you desire
// ....
}
}// end successful POST function
}); // end jQuery ajax call
// end setting up the autosave on every form on the page
}// end function autosave()
setInterval(autosave, 5000);
The problem is that I am getting different strings in the first alert and in the second one (in else section). In the first alert, the string seems to be correct with key=value&key2=value2.... but the second alert in else section gives me all html page. Is this correct behaviour? The autosave functionality doesn't work... Am I missing something?
<?php if((isset($_POST['sub'])) || (isset($_POST["autosave_check"])))
{ //save to db
}else{
?>
<form action="" method="post" id="myform" class="ajax_form" name="myform" enctype='multipart/form-data'>
<ul class="sorting dd-list" id="my_list">
//generate inputs
</ul>
<input type="hidden" name="chbox" class='hdbox' id="chbox" value="" />
<input type="hidden" name="save" value="<?php echo $idn?>" />
<input type="hidden" id="autosave_check" name="autosave_check" value="1">
<br class="clear" /> <br />
<div class="creator-buttons">
<h2 class="subheader-creator"> Akce </h2>
<table>
<tr>
<td>
<a href="javascript:;" class='radiusz addchbox' id="ch" value="Checkbox field"><?php echo $langa['formc'][10];?>
</a>
</td>
<td align='right'>
<input type="hidden" id="paths" value="<?php echo $patH?>" />
<input type="submit" name='sub' value='<?php echo $langa['formc'][13]?>' id="saveT" class='dugme1 submitforms' style="float:right;" />
</td>
</tr>
</table>
</div>
</form>
<?php } ?>
Its quite long code so just some parts.. Saving on button click works perfectly as for any other form.
I have checked this question in SOF,
How to prevent form element from sending some fields we don't want?
Here is my code
index.php
<script type="text/javascript">
$(formElement).submit(function() {
... how to do the jquery stuff..
return true;
});
<?php
$my_variable_with_value1 = 'abc';
$my_variable_with_value2 = 'def';
$another_one_variable = 'mango';
?>
<form method="post" action="process.php">
<input type="text" id="id1" name="name1" value="<?php echo $my_variable_with_value1 ?>" />
<input type="text" id="id2" name="name2" value="<?php echo $my_variable_with_value2 ?>" />
<input type="submit" value="Submit" />
</form>
I need to do the following
1. Before the page is submitted i need to unset/empty the variable values using JQUERY ONLY
2. I have to show the variable in index.php and not in the next page process.php
In other words, HOW CAN I EMPTY THE VARIABLES $my_variable_with_value1 AND $another_one_variable BEFORE PAGE SUBMIT USING JQUERY
Thanks,
Kimz
Instead of directly submitting the form using
$(formElement).submit(function() {
call an onclick function in button as:
<input type="submit" value="Submit" onclick="submitform()" />
and in the js function you can set the value of input fields to empty like:
function submitform()
{
$('#id1').val('');
$('form').submit();
}
Using below code you can set value of var1 (ID) and var2 (ID) to blank.
<script type="text/javascript">
$(formElement).submit(function() {
$("#Var1").val("");
$("#Var2").val("");
return true;
});
I'm new here and a super noob in programming. I'm having trouble with my project. My problem is that I'd like hide the form after submit and retain the data input in it.
Here's my code:
<?php
$displayform = true;
if (isset($_POST['trck']))
{
$track = addslashes(strip_tags($_POST['tracknumber']));
$ord = $_POST['id'];
$displayform = false;
if (!$track)
echo "Please enter your tracking number!";
else
{
mysql_query("update `orderdetails` set `trackno`='$track' where `id`='$ord'");
}
if ($row2['id']==$ord)
echo $_POST['tracknumber'];
}
if ($displayform)
{
?>
<form method="post" action="">
<input type="text" name="tracknumber" id="tracknumber" size="30" maxlength="30" placeholder="Enter your track number here." />
<input type="hidden" name="id" value="<?php echo $row2['id']; ?>">
<input type="submit" name="trck" id="trck" value="Save" onclick="return confirm(\'Are you sure you want to save this tracking number?\');" />
</form>
</td>
</tr>
<?php
}
}
?>
This code was inside a while loop and my problem with this is that after I submit all the form is hidden. All I want is to hide the form with the specific ID on a query.
Simplest way is to use jQuery hide
Include the jquery as
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
$("#buttonid").click(function(){
$("#formid").hide();
});
You are looking for something like this in your <form> tag:
<form method="post" action="" id="awesome_form" onSubmit="document.getElementById('awesome_form').style.display = 'none';">
use the jQuery and use :
$("#submitButtonId").click(function(){
$('#formId').hide();
});
or else in pure javascript use
<input type="submit" value="submit" onClick="hideIt()"/>
<script type="text/javascript" >
function hideIt() {
document.getElementById('formId').style.display = 'none';
}
</script>
its better not to use inline javascript
I have seen this done before but not sure how.
I am trying to have a search form go to pagename.php?q=[searchquery] so i can then get the searchquery from the address.
here is the form
<form class="sidebar-search">
<div class="input-box">
<input type="text" placeholder="Quick Product Search..." />
<input type="button" class="submit" value="" />
</div>
</form>
Here is the JS
// handle the search query submit on enter press
$('.sidebar-search input').keypress(function (e) {
if (e.which == 13) {
window.location.href = "search_results.php";
return false; //<---- Add this line
}
});
// handle the search submit
$('.sidebar-search .submit').click(function () {
if ($('.page-container').hasClass("sidebar-closed")) {
if ($('.sidebar-search').hasClass('open') == false) {
$('.sidebar-search').addClass("open");
} else {
window.location.href = "search_results.php";
}
} else {
window.location.href = "search_results.php";
}
});
Can anyone help with this?
You wouldn't actually need to do it using javascript
<form class="sidebar-search" method="get" action="search_results.php">
<div class="input-box">
<input type="text" placeholder="Quick Product Search..." />
<input type="submit" class="submit" value="" />
</div>
</form>
The action attibute defines the location (an URL) where the form's collected data should be sent.
The method attribute defines which HTTP method to send the data with (it can be "get" or "post").
This would probably help understand in detail.
https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/My_first_HTML_form?redirectlocale=en-US&redirectslug=HTML%2FForms%2FMy_first_HTML_form
But if you still need to use javascript here the answer
define an id for the search field as below
<input type="text" id="txtSearch" placeholder="Quick Product Search..." />
and then
var searchString = $('#txtSearch').val();
window.location.href = "search_results.php?q=" + searchString;
Do this:
window.location.href = "search_results.php?q=" + $(".sidebar-search input[type=text]").val();
Or you can give an id to the search and get the value of the element with that ID.
if you hit enter/submit you go to search.php?query=value
<form method="get" action="search.php" >
<input name="query" type="text" />
<input type="submit" value="search" />
</form>
Well, I have this form tag which works perfectly.
<form action="searchresult.php" method="post">
<?php
require('connect.inc');
$sql1="SELECT room_type,room_id FROM room";
$result1=mysql_query($sql1);
echo "<select name=roomtype value=''></option>";
while($nt=mysql_fetch_array($result1)){
echo "<option value=$nt[room_type]>$nt[room_type]</option>";
}
echo "</select>";
mysql_close();
?>
</td>
<td>
<input type='hidden' name='clientid' value="<?php echo $clientid; ?>">
<input type="text" class="w8em format-y-m-d highlight-days-67 range-low-today" name="chkin" id="sd" value="" maxlength="20" readonly />
</td>
<td>
<input type="text" class="w8em format-y-m-d highlight-days-67 range-low-today" name="chkout" id="ed" value="" maxlength="20" readonly />
</td>
</tr>
</table>
<input type='submit' name='submit' id="button1" value="Check Room Availabilty">
</form>
This opens up to a new page named "searchresult.php"
But,what I need to know is, how to display this .php file in a pop up style.
I am currently using facebox as my pop up,and I use it like this:
<a rel="facebox" href="searchresult.php">Check Room Availability</a>
But to no success, the inputs are not passing to the next page,so I get errors.
I know variables can be pass through the a tag like this
echo'<a rel="facebox" href=editroom.php?id=' . $row3["room_id"] . '>' . 'Edit' . '</a>';
But my case now is , I need the inputs to be pass.
I declare this codes in every page.
<link href="src/facebox.css" media="screen" rel="stylesheet" type="text/css" />
<script src="src/facebox.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('a[rel*=facebox]').facebox({
loadingImage : 'src/loading.gif',
closeImage : 'src/closelabel.png'
})
})
Thanks.
If you are using jQuery you could attach a method to the form submit to call
jQuery.facebox({ ajax: 'searchresult.php' });
You will ofcourse have to pass some URL parameters in order for it to display the relevant results
Something like this will work:
$('#search').submit(function() {
$.facebox({ ajax: 'searchresult.php?search=&search2=' });
return false;
});