문제

Good evening .. I have a form where you assign work to a worker, however, if that work is already assigned then user has a choice of either reassigning it or returning to previous page. so I'm trying to write the syntax for a confirm alert that redirects user to one page or another depending on his choice , I placed it within the head section and it goes something like this:

function show_confirm() {
    var con = confirm("Already assigned.. would you like to reassign?");
    if (con ==true) {
       window.location = "reassign.php"
    } else {
       window.location = "index.php"
    }
 }

The question is : how do you call this function depending on an if statement? I have seen some examples on the internet where people place the function (rather than just call it ) directly inside the if statement, but wouldn't that be mixing different kinds of script? So .. how do you call the function from within this if statement? I tried this but it didnt work:

$flat=$_POST['flat'];
$check= mysql_query("SELECT * FROM assignment WHERE flat = '$flat'");
$num= mysql_num_rows($check);
if ($num!=0) {   show_confirm();
 } else { //irrellivant code
도움이 되었습니까?

해결책

Since this is a JavaScript function, you just place the function itself inside the header, and then in PHP, you output the call to the function in your if like this:

if ($num!=0) {
   echo '<script type="text/javascript">show_confirm();</script>';
} else { 
   //irrelevant code
}

PHP is a server-side scripting language, while JavaScript is client-side. You can't call the JavaScript function directly from the server-side like you were trying to do. You can however output javascript to the page via PHP as I have done in my example.

다른 팁

Yes, you are trying to mix php (server side) and javascript (client side) in one process which won't work.

When you submit the form to check if they are already assigned you then need to redirect them to a confirm page, from there you would have yes/no buttons linking to the reassign and go back pages.

The best solution would actually be to handle this with AJAX but given the nature question I think its best to stick with non-ajax workflow.

Let me know if that makes sense, if not I will add some more detail

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top