Fragrance Resources
Fragrance Resources

Reputation: 37

return method for form submission in javascript

developers i create one page where it fetches data from the registration page. Each data row I put add and unfriend button (disabled). Once the user clicks add, the prompt box appears to ask the user to enter a subject and click ok. After click ok,it insert in another database table.while the unfriend button will be able to click. Here my problem is once click add button the prompt appears and after click ok, the data does not insert into the database. If I click unfriend button it inserts into the database. I want the data submitted whenever the user clicks the add button. I think it's because this form has two submit buttons but I don't know how to distinguish between the buttons.Moreover,in javascript i put return false and true follow some tutorials.may i know when i should use return false and true?. Here is the code:

<?php
session_start();
$mysqli=new MySQLi('127.0.0.1','root','','learning_malaysia');
$sql = "SELECT * FROM tutor_register INNER JOIN tutorskill ON tutor_register.register_ID = tutorskill.register_ID";
$result= mysqli_query($mysqli,$sql);
?>
<html>
<script>
function myFunction(form){
    var subject = prompt("Please enter Subject that want to study");
    form['add'].value="request sent";
    if (subject != null){
        form['subject'].value= subject;
        form['btn'].disabled=false;
    form['add'].disabled=true;
    return false;
    form.submit();
}
return true;
form['add'].submit();
}
function unfriend(form){
 form["add"].disabled=false;
        form["btn"].disabled=true;
    form["add"].value="Add friend"; 
    return true;
    form.submit();
}
</script>
<body>
<?php
if(mysqli_num_rows($result)>0)
{

    while($row = mysqli_fetch_array($result))
{      
        $register_ID=$row["register_ID"];
    ?>
 <form method="post" id="form" enctype="multipart/form-data" autocomplete="off"> 

                   <input type="hidden" name="id" value="<?php echo $row['register_ID'];?>" />
                 <input type="hidden" id="subject" name="subject" data-uid=<?php echo $_SESSION['sid'] ;?>/>
                 <td><input type="submit" onclick="return myFunction(this.form);" name="addfriend" data-type='addfriend' id="add" class="btn" value="add"  /> 
                 <input type="submit" value="unfriend" id="btn"  onclick="return unfriend(this.form);" disabled /> </td>   </form>
<?php
            }
}
?>
<?php
if(isset($_POST['subject']) and $_POST['id']) {
$user_id = $_SESSION['sid'];
$friend_id = $_POST['id'];
$status = "yes";
$subject=$_POST['subject'];
$sql="INSERT INTO friends(user_id,status,subject,friend_id)" ."VALUES('$user_id','yes','$subject','$friend_id') ";

            if($mysqli->query($sql)=== true) {
                          $_SESSION['status']="yes";
                         $_SESSION['friend_id']=$friend_id;
                         $_SESSION['user_id'] = $user_id;

            } else {

                }
                              }?>                    
    </body>
</html>

Upvotes: 0

Views: 1376

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074485

Your return statements terminate the function; no code after the return will execute, so your form.submit() calls never happen. In your friend function, because you're returning false and your onclick has return friend(...), you're cancelling form submission, and the form is never submitted (not by your code, and not by the browser), In unfriend, though, you're returning true, so although your code doesn't submit the form, the browser does.

If you want to submit the form programatically, put those form.submit calls before the return, and return false so the browser doesn't also submit the form.

Upvotes: 1

Related Questions