-1

I have a php code as shown below in which there is an if condition.

php:

if(mysqli_num_rows($result1) > 0){   // Line X
    while($row = mysqli_fetch_array($result1)){
        if($row['open'] == "true") {
                if(!isset($_SESSION['admin'])) {
                    $message = "user " . $row['user_name'] . " is currently editing the form. Want to take over ?";  // Line B
                    echo "<script type='text/javascript'>if(confirm('$message')) {   } else {  };</script>";   // Line A                        
                    }
            break;
        }
    }
}

I was able to integrate $message at Line B.

I am wondering what changes I should do at Line A so that I can integrate the following code in the curly braces of the if(confirm('$message') block:

$username = $_SESSION['user_name'];
$open="true";
$write="1";
$stmt=$connect->prepare("UPDATE trace_users SET write=0 WHERE write=1"); // revoke write access of all users
$stmt=$connect->prepare("UPDATE trace_users SET open=?, write=? WHERE user_name=?");
$stmt->bind_param('sss', $open, $write, $username);
$stmt->execute();
5
  • 1
    PHP runs on the server, JS on the client. So the only way to “get PHP code to run inside a JS conditional” is to have the JS (itself, inside the conditional) make another request to the server which can then be handled in PHP. XHR is a common method as are HTML form submissions. Commented Apr 29, 2020 at 4:06
  • At Line A, I will be getting an alert message "user User A is currently editing the form. Do you want to take over ?" Ok and Cancel. On clicking Ok, I want the php code to be executed. Commented Apr 29, 2020 at 4:10
  • 1
    You should go and make sure you understand what is explained in What is the difference between client-side and server-side programming? first of all. (Currently, it does not give the impression that you actually do?) Commented Apr 29, 2020 at 7:33
  • @CBroe I do understand the difference between client-side and server-side programming language. I will explain my question in detail I want only one user to edit the form in php. When any user is logged in then I assume he/she is editing the form in php. Commented Apr 29, 2020 at 16:23
  • When 2nd user wants to login at the same time then the alert message will say, user User A is currently editing the form. Want to take over ? Ok and Cancel checkbox will appear then. Ok clicking, I have to login 2nd user so I need to put sql query inside the curly braces of if block. Is my approach right ? Commented Apr 29, 2020 at 16:27

2 Answers 2

1

Unfortunately user1950349, this is a bit more complex than it may seem because your idea is closer to how a desktop app would where there is a message loop which allows the code to respond to user input. But with a webpage generated by php and sent over the internet, we do not have a message loop to work with. Thus, we have figure out a way to get the server and the user's computer to message back and forth without any additional webpage loads.

This is where AJAX would come into play; however, an explanation of how to use that for your use case would depend on how you implement AJAX. For instance, you might use jQuery or some other javascript library.

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

Comments

0

It is a good practice if you avoid mixing two different languages together.

When mixing these two the complexity of your code increases.

Since you need the answer here you go

echo "<script type='text/javascript'>if(confirm(" . $message . ")) {   } else { };</script>";

Inside echo's double/single quotes(" / ') everything is considered as a string, so to access your PHP variable you need to use the concatenation method to use that PHP variable.

if(confirm(" . $message . "))

UPDATE

You need to follow the same steps, as described above. You need to use the string concatenations.

But that will be very complex and as well as very difficult to make debugging.

You can use a better approach using XHR request or you can design the PHP code in a better way to use it with your JavaScript code.

2 Comments

I have modified my question. I meant to say in the curly braces of the if block.
@user1950349 The same concatenations you can use, but that will be very much complex, so the better approach is to make few modifications in your code, I have updated the answer please have a look.

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.