-4

I have an auto-generated form which submits data using ajax as shown below in joomla CMS:

Please note that i cannot make any modifications to this form else it will not submit data anymore. Also the composer_965292549 changes every time the page loads.

<form name="dc_submit" autocomplete="off" class="form-horizontal" action="/demo?controller=posts&amp;task=reply" method="post">
First name: <input type="text" name="fname">
<input type="submit" value="Submit" onclick="discuss.reply.submit('composer_965292549');return false;">
</form>

Now i want to add a javascript alert and here's the code

<script type="text/javascript">
function discuss.reply.submit()
{
  alert("his");
  }
</script>

Now, this javascript alert doesn't work. Replacing discuss.reply.submit() with discuss() tends to do the trick but if i do that, the data never reaches the database. I can't replace that. Please suggest.

user3362364
  • 441
  • 11
  • 25
  • Because that is not a valid function name. – epascarello Feb 27 '14 at 21:11
  • 1
    Can't use dots in function name, and you should probably override the click action and do the XMLHttpRequest yourself – Andrew Templeton Feb 27 '14 at 21:13
  • Yes. but the form onclick="discuss.reply.submit('composer_965292549');return false;" already existed and i can't change that. Can you suggest any other method where i could have an alert box on submit? – user3362364 Feb 27 '14 at 21:14
  • iscuss.reply.submit('composer_965292549');alert("his"); return false; – LasagnaAndroid Feb 27 '14 at 21:15
  • 2 negative votes?!! seriously? I'm trying to find a way to put an alert on form submit. I can't change the fields since it is an auto-generated form. Help me out here for any alternative methods instead. – user3362364 Feb 27 '14 at 21:17

4 Answers4

1

What exists is that is based off an object

discuss = discuss || {};
discuss.reply = discuss.reply || {}
discuss.reply.submit = function() {
   alert("xxx");
}

or if the code already exixts, you can just override it

discuss.reply.submit = function() {
   alert("xxx");
}

This will override the default function. Meaning, it will not do what it did before. If you still want this to be called you can copy the old one and call it.

var _org = discuss.reply.submit;
discuss.reply.submit = function() {
   alert("xxx");
   _org.apply( this, arguments );
}
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Thank you. This seems to submit the form but its conflicting and it comes back with an error, "the system was not able to detect the id". Any thoughts? – user3362364 Feb 27 '14 at 21:23
0

The function name is discuss.reply.submit. This is not a valid function name, see this stackoverflow post on variable names, the same rules apply. A good function name would be onDiscussReplySubmit.

Community
  • 1
  • 1
PlasmaPower
  • 1,864
  • 15
  • 18
0

Your function name implies a structure such as

 var discuss ={ "reply" : { "submit" : function(){alert("his");}}}

This is an object not a function name. Either change your function name or make an object.

mccainz
  • 3,478
  • 5
  • 35
  • 45
-1

First, you can't use dots on JS function names.

<script type="text/javascript">
function discussReplySubmit(x)
{
  alert(x);
}
</script>

Second, if you don't wanna to have the form submited, them replace submit with a button:

<input type="button" value="Submit" onclick="discussReplySubmit('composer_965292549')" />

Finally, if you want to submit the form don't use onclick event on submit button, use the OnSubmit event of Form:

<form name="dc_submit" autocomplete="off"
    class="form-horizontal" action="/demo?controller=posts&amp;task=reply"
    method="post" OnSubmit="discussReplySubmit('composer_965292549')">
    First name: <input type="text" name="fname">
    <input type="submit" value="Submit" />
</form>
rkawano
  • 2,443
  • 22
  • 22