Yii Framework Forum: Run Javascript before Submit - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Run Javascript before Submit

#1 User is offline   nadji25023 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 14-June 12

Posted 07 July 2012 - 08:06 AM

Assalemou 3alaykom,


I'm newbie in Yii && PHP/Javascript. I want to run a Javascript function before submit my Form

Do some thing like that in Yii

<html>
<head>
<script>
function doThis()
{
alert("testing");
return true
}
</script>
</head>
<body>
<form action="test.php" onsubmit="return doThis()" method="post">
<input type=submit >
</form>
</body>
</html>


Any body can help me how to implement that in Yii ?

Many thanks ya ikhwan :D
0

#2 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,168
  • Joined: 05-January 12
  • Location:Morocco

Posted 12 July 2012 - 02:11 PM

Wa 3alaykum assalam wa arra7ma

A good example lays in the "admin.php" view that is automatically generated by gii/giix:

<?php Yii::app()->clientScript->registerScript('someScript', "
$('#testForm').submit(function() {
    alert("testing");
});
");
?>

<form id="testForm" action="test.php" method="post">
    <input type=submit >
</form>

0

#3 User is offline   Sampa 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 18-December 11
  • Location:Stockholm,Sweden

Posted 24 July 2012 - 01:04 PM

Do it like this:
<html>
<head>
<script>
$("#myform").submit(function(event){
event.preventDefault();
doThis();
$("#myform").submit();
});
function doThis()
{
alert("testing");
return true
}
</script>
</head>
<body>
<form id="myform" action="test.php" method="post">
<input type=submit >
</form>
</body>
</html>

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users