Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
save_group.php

PHP
<?php
    include('../dbconnection.php');
    
    $ruser = $_POST['r_user'];
    $pid = $_POST['p_id'];
    
    $conn->query("INSERT INTO `new_r_login_p_id` VALUES ('', '$pid', '$ruser')");
     
		
?>


group_validator.php


PHP
<?php
	include('../dbconnection.php');
	
	$ruser = $_POST['r_user'];
    $pid = $_POST['p_id'];
	$query = $conn->query("SELECT * FROM `new_r_login_p_id` WHERE `r_user` = '$ruser' && `p_id` = '$pid'");
	$validate = $query->num_rows;
	if($validate > 0){
		echo "Success";
	}else{
		echo "Error";
	}
	
	?>



script.js

JavaScript
$(document).ready(function() {
    $error = $('<center><label class = "text-danger">Please Fill up the form!</label></center>');
    $load_status = $('<center><label class = "text-success">Waiting...</label></center>');
    $group_valid = $('<center><label class = "text-danger">Member already joined!</label></center>');
   

$('#save_group').click(function(){
    $error.remove();
    $group_valid.remove();
    $ruser = $('#usermember').val();
    $pid = $('#project').val();
    alert("You had me at hello");
    if($ruser == "option" ){
        
        $error.appendTo('#loading');
    }else{
        $load_status.appendTo('#loading');
        setTimeout(function(){
            
            console.log("group_validator got loaded");
            $.post('group_validator.php', {r_user : $ruser, p_id : $pid},
                function(result){
                    if(result == "Success"){
                        alert("LOADED");
                        console.log("$.post call done");
                        console.log("result = " + result);
                        $group_valid.appendTo('#loading');
                        
                    }else{
                        $.ajax({
                            type: 'POST',
                            url: 'save_group.php',
                            data: {r_user : $ruser, p_id : $pid},
                            success: function(){
                                window.location = 'group.php?pid=' + $pid;
                            }
                        });
                    }
                }
            )
        $load_status.remove();	
        }, 3000);
    }
});

});


group.php

PHP
<div class="col-sm-4 text-white text-center mt-5">
                        <div class="card box p-2 text-white mb-3">
                            <div class="card-header bg-primary text-white">User(s)</div>
                            <div class="card-body">
                                <form id="form" method="POST">
                                <div class="form-group" style="color:#5777ba;">
                                    <label for="inputprojectuser" style="color:#5777ba;">Choose any one:</label>

                                    <select id="usermember" class="form-control chosen-select" data-style="btn-primary" >
                                        <option value="option">Select a member</option>

                                        <?php
                                        $sqli = mysqli_query($conn, "SELECT r_login_id, r_name FROM userregistration_db WHERE r_select = 'user'");
                                        
                                        while ($row = mysqli_fetch_array($sqli)) {
                                            echo "<option value=" . $row['r_login_id'] . ">" . $row['r_name'] . "</option>";
                                        }
                                        ?>

                                    </select>
                                    
                                    <input type="hidden" id="project" value="<?php echo $c_fetch['p_id'] ?>" >

                                </div>

                                
                            </div>
                        </div>
                    </div>


<div class="col-sm-12 mb-5">

                       <button type="button" id="save_group" class="btn btn-primary btn-large btn-block">Add User(s)</button>
                   </div>

                   </form>


What I have tried:

I've tried add this code

group_validator.php
PHP
setTimeout(function() {
    $.post('group_validator.php', { r_user: $ruser,r_developer: $rdeveloper, r_pmanager: $rpmanager, p_id: $pid },
    function(result) {
        console.log("$.post call done");
        console.log("result = " + result);

    });
    $load_status.remove();
}, 3000);


It seems to be gives this in console
$.post call done
script.js:28 result = connect
Success
when it checks the user in the database
script.js:27 $.post call done
script.js:28 result = connect
Error

when the user is not added in the database

I have spended way too much time , i just need to complete this as soon as possible
Guys please help me out
Is there something i'm missing
Posted
Updated 3-Jan-21 4:15am
v3
Comments
[no name] 3-Jan-21 11:27am    
How does "result" get loaded? Is "echo" supposed to do that?
nimisha dubey 3-Jan-21 11:36am    
yes."echo" is supposed to do that. Also im trying to add save_group code in the script
$.ajax({
type: 'POST',
url: 'save_group.php',
data: {r_user : $ruser, p_id : $pid},
success: function(){
window.location = 'group.php?pid=' + $pid;
}
})
but its not working. any idea how can i do that
Richard Deeming 6-Jan-21 10:21am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900