This is a discussion on php-code in javascript within the PHP General forums, part of the PHP Programming Forums category; hello everybody, I have a newbie question: Is it possible to use php-code in een javascript function? In this ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
hello everybody, I have a newbie question:
Is it possible to use php-code in een javascript function? In this way the messages are deleted, but the alert en confirm windows don't apper. Something like: <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html;charset=iso-8859-1"/> <style type="text/css" media="screen"><!-- --></style> <script language="JavaScript" type="text/javascript"> function delete_message() { var win=window.confirm("Do you want to delete this message?") if(win==true) { <?php php-code to connect to the database and to delete a record ?> window.alert("Message deleted!") }else{ window.alert("The message is not deleted.") } } </script> </head> <body> <a href="#" onClick="skip_bericht()">delete</a> </body> </html> |
|
|||
|
edt wrote: > hello everybody, I have a newbie question: > Is it possible to use php-code in een javascript function? > In this way the messages are deleted, but the alert en confirm windows don't > apper. > Something like: > > <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> > <html xmlns="http://www.w3.org/1999/xhtml"> > <head> > <meta http-equiv="content-type" content="text/html;charset=iso-8859-1"/> > <style type="text/css" media="screen"><!-- > --></style> > > <script language="JavaScript" type="text/javascript"> > function delete_message() { > var win=window.confirm("Do you want to delete this message?") > if(win==true) { > <?php > php-code to connect to the database and to delete a record > ?> > window.alert("Message deleted!") > }else{ > window.alert("The message is not deleted.") Remember, PHP executes on the server and Javascript on the client. In your example, by the time the client sees you Javascript code, PHP has is all done. The only way I know that can do what you want is to use Javascript XMLHttp methods to start another PHP script and wait for it to finish. Ken |
|
|||
|
Ken Robinson wrote:
> > edt wrote: >> hello everybody, I have a newbie question: >> Is it possible to use php-code in een javascript function? >> In this way the messages are deleted, but the alert en confirm windows >> don't apper. >> Something like: >> >> <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" >> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> >> <html xmlns="http://www.w3.org/1999/xhtml"> >> <head> >> <meta http-equiv="content-type" content="text/html;charset=iso-8859-1"/> >> <style type="text/css" media="screen"><!-- >> --></style> >> >> <script language="JavaScript" type="text/javascript"> >> function delete_message() { >> var win=window.confirm("Do you want to delete this message?") >> if(win==true) { >> <?php >> php-code to connect to the database and to delete a record >> ?> >> window.alert("Message deleted!") >> }else{ >> window.alert("The message is not deleted.") > > Remember, PHP executes on the server and Javascript on the client. In > your example, by the time the client sees you Javascript code, PHP has > is all done. The only way I know that can do what you want is to use > Javascript XMLHttp methods to start another PHP script and wait for it > to finish. > > Ken Ken, thanks for the answer! I will try it the other way. Ed |