nithi
nithi

Reputation: 3877

jquery form submit with dynamic id not working

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.

Upvotes: 0

Views: 1359

Answers (3)

tshenolo
tshenolo

Reputation: 463

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

Upvotes: 0

Reporter
Reporter

Reputation: 3948

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.

Upvotes: 1

ElonU Webdev
ElonU Webdev

Reputation: 2459

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.

Upvotes: 0

Related Questions