0

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>

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the reply. i try as you mention. Now it can submit. but another problem is button enable and disable is not working..
i try like this <script> function myFunction(form){ var subject = prompt("Please enter Subject that want to study"); form['add'].value="request sent"; form['btn'].disabled=false; form['add'].disabled=true; if (subject != null){ form['subject'].value= subject; form.submit(); return false; }} function unfriend(form){ form["add"].disabled=false; form["btn"].disabled=false; form["add"].value="Add friend"; form.submit(); return true; } </script>
@FragranceResources - That's a different question. You should post it as one (once you've tried to resolve the problem yourself).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.