I can't submit the form with dynamic id. Below is my code.
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>
<script type="text/javascript">
var fval;
var cvalue = "<?php echo $_POST['currentval']; ?>";
if(!(cvalue)) cvalue=0;
$(document).ready(function() {
$('#upload'+cvalue).submit(function() {
var options = {
target: '#message',
url:'process.php?sval='+cvalue,
success: function() {
alert("success");
$('#uploader').html('');
}
};
$(this).ajaxSubmit(options);
return false;
});
});
</script>
<div id="message"></div>
<?php
for($i=0;$i<5;$i++) {
echo "<form action='#' method='post' name='upload' id='upload$i' enctype='multipart/form-data'>";
echo "<input type='hidden' name='svalhid' id='svalhid' value='$i'>";
echo "<input type='file' id='fl$i' name='filename".$i."up'>";
echo "<input type='hidden' name='currentval' id='currentval' value='$i'>";
echo "<input type='submit' name='uploads$i' value='Ok'><br>";
echo '</form>';
}
?>
In this line, $('#upload'+cvalue).submit(function() {
I can't get the cvalue. I can't identify what's wrong with this code. Anybody please help me.
Check follow line
echo "<form action='#' method='post' name='upload' id='upload$i' enctype='multipart/form-data'>";
Are you really sure that you printed out a correct value for the id attribute?
I think it exists two better ways:
1st (the best way in my eyes):
<?php
for($i=0;$i<5;$i++) {
?>
<form action='#' method='post' name='upload' id='upload<?php echo $i ?>' enctype='multipart/form-data'>";
<input type='hidden' name='svalhid' id='svalhid' value='<?php echo $i ?>'>"
<input type='file' id='fl<?php echo $i ?>' name='filename<?php echo $i ?>up'>";
<input type='hidden' name='currentval' id='currentval' value='<?php echo$i ?>'>"
<input type='submit' name='uploads<?php echo $i ?>' value='Ok'><br>"
</form>'
<?php
}
?>
2nd: Change the mentioned line to following:
echo "<form action='#' method='post' name='upload".$i."' id='upload".$i."' enctype='multipart/form-data'>";
Note: You defined a couple of forms with the same name. As far as I know, the attribute 'name' is the main attribute for forms, not the 'id' attribute.
I don't see where you assign anything besides "" or 0 to cvalue. Also, those two assignments are outside of $(document).ready(function() { so there could be something weird going on with that code executing before your document has fully loaded. Move them inside of the document.ready call and see what happens.
Try using
var cvalue = "<?php echo $_GET['currentval']; ?>";
or
var cvalue = "<?php echo $_REQUEST['currentval']; ?>";
Also try what #reporter said about the form id: id='upload".$i."
The form id should be the same as the one referenced here:
$('#upload'+cvalue).submit(function() {
Example:
in javascript:
$('#upload').submit(function() {
then form should be:
<form id="#upload" ...
You should definitely see something happen.
Try installing firebug and use console.log(variablehere) instead of alert(variablehere). It's more cleaner.
Hope that helps
Related
I am trying to use an ajax form submission for a list of forms generated by a php foreach loop. My issue is I obviously need to mark each generated form as unique and can't seem to figure it out. I thought to use the HTML data- attribute but cant seem to make it work.
Here is the code so
<?php
if (isset($_SESSION['team_mem_id'])) {
$query_team_matches = $conn->prepare($sql_team_matches);
$query_team_matches->bindParam(':tid', $tid, PDO::PARAM_INT);
$query_team_matches->execute();
$matches = $query_team_matches->fetchAll(PDO::FETCH_ASSOC);
if (!empty($matches)) {
foreach($matches as $Matches) {
$tmid = $Matches['tmid'];
$opponent = $Matches['opponent'];
$location = $Matches['location'];
$tm_datetime = $Matches['tm_datetime'];
$match_date = date("F j",strtotime($tm_datetime));
$match_time = date("g:i A",strtotime($tm_datetime));
$match_when = $match_date. " # ".$match_time;
?>
<div class='match_container'>
<div class='match_basic_info'>
<h3><?php echo $match_when.' - <span style="font-size: 16px">'.$location.' vs '.$opponent; ?></span></h3>
<a class='match_details_link' href='http://localhost:1234/tennisexcel/teams/match.php?r=team&tid=<?php echo $tid; ?>&tmid=<?php echo $tmid; ?>'>More info...</a>
</div>
<div id='match_avail_container' class='match_avail_container'>
<h3>My availability:</h3>
<form id='avail_submit_form' data-tmid='<?php echo $tmid; ?>' class="" action='http://localhost:1234/tennisexcel/test_ajax.php' method='POST'>
<input type='hidden' name='uid' value='<?php echo $_SESSION['uid']; ?>'/>
<input type='hidden' name='tid' value='<?php echo $_SESSION['tid']; ?>'/>
<input type='hidden' name='tmid' value='<?php echo $tmid; ?>'/>
<button class='match_avail_yes' type='submit' name='availability' value='1'>✔</button>
<button class='match_avail_no' type='submit' name='availability' value='2'>✖</button>
</form>
<div id="signup" data-tmid='<?php echo $tmid; ?>' class='match_avail_success success_display'>
<p>success!</p>
</div>
</div>
</div>
<?php
}
}
}
?>
I added a data-tmid='$tmid' to the form and the div that need to be marked as unique. The jquery is below:
$(document).ready(function() {
$('#avail_submit_form').submit(function(evt) {
evt.preventDefault();
var url = $(this).attr('action');
var fromData = $(this).serialize();
$.post(url, fromData, function(response) {
$('#avail_submit_form').hide();
$('#signup').removeclass('success_display');
}); //end post
}); //end submit
}); //end ready
I tried adding on [data-tmid='+$(this).attr('data-tmid')+'] to each id but not sure how to make it work properly or if it is even the correct way to do it.
Thank anyone who can lead me in the right direction!
Use a class instead of ID.
<div class='match_container'>
<div class='match_basic_info'>
<h3><?php echo $match_when.' - <span style="font-size: 16px">'.$location.' vs '.$opponent; ?></span></h3>
<a class='match_details_link' href='http://localhost:1234/tennisexcel/teams/match.php?r=team&tid=<?php echo $tid; ?>&tmid=<?php echo $tmid; ?>'>More info...</a>
</div>
<!-- fix #1 -->
<div class='match_avail_container'>
<h3>My availability:</h3>
<!-- fix #2 -->
<form data-tmid='<?php echo $tmid; ?>' class="avail_submit_form" action='http://localhost:1234/tennisexcel/test_ajax.php' method='POST'>
<input type='hidden' name='uid' value='<?php echo $_SESSION['uid']; ?>'/>
<input type='hidden' name='tid' value='<?php echo $_SESSION['tid']; ?>'/>
<input type='hidden' name='tmid' value='<?php echo $tmid; ?>'/>
<button class='match_avail_yes' type='submit' name='availability' value='1'>✔</button>
<button class='match_avail_no' type='submit' name='availability' value='2'>✖</button>
</form>
<!-- fix #3 -->
<div data-tmid='<?php echo $tmid; ?>' class='match_avail_success success_display'>
<p>success!</p>
</div>
</div>
</div>
I am assuming you only want to target the current form in your jQuery
$(document).ready(function() {
// add event handler to your forms
$('.avail_submit_form').submit(function(evt) {
evt.preventDefault();
// cache the current form object
var form = $(this);
var url = form.attr('action');
var fromData = form.serialize();
$.post(url, fromData, function(response) {
// make changes to current form and its related element
form.hide();
form.next('.match_avail_success').removeclass('success_display');
});
});
});
I have following jquery script which load a form into a div "blankform"
jQuery('#metentry').submit(function(e){
e.preventDefault();
// getting data from form to variables
var date = jQuery('#stddate').val();
var kgsunit = jQuery('#unit').val();
var kgsshift = jQuery('#shift').val();
//sending data to script
jQuery.post("get_blank_form.php", {
"date": jQuery('#stddate').val(),
'unit': kgsunit,
'shift': kgsshift,
}, function(data) {
//loading a form to the div blankform
jQuery('#blankform').html(data);
});
get_blank_form.php contains following code
<?php
echo'<form id="shiftentry" name="shiftentry" >';
echo "Date:<input id=shiftdate value='".$date."'/>";
echo "Unit:<input id=shiftdate value='".$unit."'/>";
echo "Shift:<input id=shiftdate value='".$shift."'/><br/>";
echo "<table><tr><th>No</th><th>Ele</th><th>Location</th><th>Dose Rate (mGy/h)</th><th> Tritium (DAC)</th> <th>Part (DAC)</th> <th>Iodine (DAC)</th><th> Surface Cont. (Bq/cm2) </th></tr>";
while($row2 = mysql_fetch_array($result_sec))
{
echo "<tr>";
echo "<td>".++$rc."</td>";
echo "<td><input size='5' id='".$row2['elevation']."' value='".$row2['elevation']."' /></td>";
echo "<td><input id='".$row2['loc_id']."' value='".$row2['location']."' /></td>";
echo "<td><input size='5' id='dose".$rc."' value='".$row2['radn_level']."'/></td>";
echo "<td><input size='5' id='h3".$rc."' value='0' /></td>";
echo "<td><input size='5' id='part".$rc."' value='0' /></td>";
echo "<td><input size='5' id='iod".$rc."' value='0' /></td>";
echo "<td><input size='5' id='cont".$rc."' value='0' /></td>";
echo "</tr>";
}
echo " </table>";
echo '<div align="center">';
echo '<input type="submit" id="submit" name="submit" value="Submit" width="30" />';
echo '</div>';
?>
this will create a form which will have input like id1, id2 ... id25, dose1, dose2 ... dose25 and so on.
I want to read the values of these input boxes. Normal method like var loc1 = $("#id1").val(); is not working since these elements are dynamically created.
Even the submit button clicking even is also not triggered.
the script below do not produce any output.
jQuery('#shiftentry').submit(function(e) {
e.preventDefault();
alert("Submitted");
Any suggestions please?
Since the Form DOM is dynamically created after Ajax Call, You can use .on ?
like:
$(document).on('submit' , '#shiftentry' , function(e) {
e.preventDefault();
alert("Submitted");
});
You will need to use on() method for event handling as the form is created using ajax call and added later.
As it's added later, it will not be bound to submit event handler that you had specified. To bind it with the handler you need to use on() from jQuery.
$(document).on('submit', '#shiftentry', function() {
//Your code
});
As for the submit button, you can trigger it using
$('#submit_id').on('click', function(){
$('#shiftentry').send(function(e) {
e.preventDefault();
alert("Submitted");
);
})
as for getting the values using.
$('#id1').val()
this should just work just fine but remember that some characters are not permitted in id field such as [, ], . etc.. if you cant avoid this special characters in your id field you can use.
$('input[name="value"]').val();
to get the value of your fields.
in the QandATable.php I have a form below which contains a file input and an iframe:
var $fileVideo = $("<form action='videoupload.php' method='post' enctype='multipart/form-data' target='upload_target_video' onsubmit='return videoClickHandler(this);' class='videouploadform' >" +
"Video File: <input name='fileVideo' type='file' class='fileVideo' /></label>" +
"<input type='submit' name='submitVideoBtn' class='sbtnvideo' value='Upload' /></label>" +
"<p class='listVideo' align='left'></p>" +
"<iframe class='upload_target_video' name='upload_target_video' src='/' style='width:0px;height:0px;border:0px;solid;#fff;'></iframe></form>");
Now below I have a jquery code which is triggered when the file has finished uploading.
function stopVideoUpload(success, videofilename){
var result = '';
videocounter++;
if (success == 1){
result = '<span class="videomsg'+videocounter+'">The file was uploaded successfully</span>';
$('.listVideo').eq(window.lastUploadVideoIndex).append('<div>' + htmlEncode(videofilename));
}
return true;
}
Now the result of the file upload and the file name is determined with the code below. But what my question is that how can I display the $id variable in a text input?
<script language="javascript" type="text/javascript">
window.top.stopVideoUpload(<?php echo $result; ?>,'<?php echo $id . $_FILES['fileVideo']['name'] ?>');
</script>
I might be misunderstanding your question, but if you change type='text' to type='hidden' on your input field, then the $id variable will be in a hidden input.
I am trying to submit a form with ajax. Here is my complete code
<?php
$wparent = "123";
$method = "sms";
?>
<script type="text/javascript">
$(document).ready(function(){
$("#post_<?php echo $wparent;?>").click(function(){
$('#parentpost-<?php echo $wparent;?>').html('Loading.....');
$("#parentpost-<?php echo $wparent;?>").load("<?php echo SITE_URL;?>new_ajax/post_reply.php", {message:$("[name=replynote]").val(), method:$("[name=method]").val(), parent:$("[name=parent]").val()}); //end
}); // end of the main click function
});
</script>
<?php
echo "<textarea name='replynote' ></textarea>";
echo "<input type=\"submit\" class=\"post_button\" id=\"post_$wparent\" value=\"post\" />";
echo "<input type=\"button\" class=\"cancel_button\" value=\"Cancel\" />";
?>
<input type='hidden' name='parent' value='<?php echo $wparent;?>' />
<input type="hidden" name="method" value="<?php echo $method;?>" />
When I checked the post variables with firebug I saw that, its sending only the method correctly. All other values are sent as undefined. I could not find the error till now.
Try pre-populating the data map that you're passing:
$(document).ready(function () {
$("#post_<?php echo $wparent;?>").click(function () {
var message = $("[name=replynote]").val();
var method = $("[name=method]").val();
var parent = $("[name=parent]").val();
var data = {
"message": message,
"method": method,
"parent": parent
};
$('#parentpost-<?php echo $wparent;?>').html('Loading.....');
$("#parentpost-<?php echo $wparent;?>").load("<?php echo SITE_URL;?>new_ajax/post_reply.php", data); //end
}); // end of the main click function
});
If data is an object containing undefined values, then your jQuery selectors aren't working.
Also, since you're only using a name attribute to find your inputs, you may want to be more specific in the selectors.
Finally, the attribute selectors typically need the value of the attribute enclosed in quotes, so try these for your selectors:
var message = $('textarea[name="replynote"]').val();
var method = $('input[name="method"]').val();
var parent = $('input[name="parent"]').val();
Hi All First of all I don't know Javascript, I got this script from the internet but can't get it to work. If someone can please be so kind and assist me. I'm trying to create a "check all" & "uncheck all" button on my form for my checkboxes, if I click the button it does not select anything. Where am I going wrong?
<?php
session_start();
?>
<head>
<SCRIPT LANGUAGE="JavaScript">
<!--
<!-- Begin
function CheckAll(chk)
{
for (i = 0; i < chk.length; i++)
chk[i].checked = true ;
}
function UnCheckAll(chk)
{
for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
}
// End -->
</script>
</head>
And then the form:
$sql0 = "SELECT * FROM .....
$result0 = mysql_query($sql0);
echo "<form name='myform' action='...' method='post'>";
while($row0 = mysql_fetch_assoc($result0)) {
$test_id=$row0['id'];
$test_name=$row0['test_name'];
echo " <table width=600px>";
echo " <tr>";
echo " <td width=30px><input name='question[$test_id][]' type='checkbox' value='1' /></td>";
echo " <td width=580px align=left>$test_name</td>";
echo " </tr>";
echo " </table>";
}
echo "</P>";
echo "<input type='button' name='Check_All' value='Check All' onClick='CheckAll(document.myform.check_list)'>";
echo "<input type='button' name='Un_CheckAll' value='Uncheck All' onClick='UnCheckAll(document.myform.check_list)'>";
echo "</form>";
if you don't know javascript, i'd strongly recommend you investigate jquery - a library which eliminates cross browser issues and makes a huge amount of javascript far easier.
simply include this:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
and your code
<input type='button' name='Check_All' value='Check All' onClick='$(":checkbox").attr("checked",true);'>
untested, but should be okay.
I'd highly recommend using jQuery, it allows you to focus on the logic and not worry about cross browser issues you might run into.
<script src='http://code.jquery.com/jquery-1.6.3.min.js' type='text/javascript'></script>
<script>
$(document).ready(function () {
//check all
$('input[name="Check_All"]').click(function () {
//for all checkboxes where the name begins with "question", check them
$('input[name^="question"]').attr('checked', true);
});
//uncheck all
$('input[name="Un_CheckAll"]').click(function () {
//for all checkboxes where the name begins with "question", uncheck them
$('input[name^="question"]').attr('checked', false);
});
});
</script>
Here's the breakdown...
Select all buttons on the page where the name attribute is set to Check_all
$('input[name="Check_All"]')
When the found item(s) are clicked, execute the code in the function that is passed
$('input[name="Check_All"]').click(function () {
//javascript code here
});
for all checkboxes where the name begins with "question", check them
$('input[name^="question"]').attr('checked', true);
Change this:
<td width=30px><input name='question[$test_id][]' type='checkbox' value='1' /></td>";
to this:
<td width=30px><input name='check_list[]' type='checkbox' value='1' /></td>";