Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6547345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:49:12+00:00 2026-05-25T11:49:12+00:00

Is it unsecure to embed PHP code in a javascript function? My friend told

  • 0

Is it unsecure to embed PHP code in a javascript function?
My friend told me not to do it.

My script just inserts a number in the database if the message has been clicked (read).

<!--Insert into database when click-->
<script>
    function insert()
    { 
        <?php
        include 'db_connect.php';
        $usermsg = $_SESSION['username'];
        $message_id = $_GET['messageid'];
        mysql_query("UPDATE messages SET message_read='1' WHERE id='$message_id' AND to_user='$usermsg'");
        ?> 
    }
</script>

Should i do this any otherway? Or drop including php & mysql in my script and start over?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:49:13+00:00Added an answer on May 25, 2026 at 11:49 am

    If you try that code, it won’t even work that way. You cannot embed server side code in javascript function.

    What you want is to make a sepearate request that will handle the request. This method is called AJAX. With jQuery library you can make AJAX POST request like this:

    <script>
        function insert()
        { 
           //Example: Request the test.php page and send some additional data along (while still ignoring the return results).
           $.post("test.php", { messageid: "1" } );
        }
    </script>
    

    In test.php:

    <?php 
    
      //Get Post Variables. The name is the same as 
      //what was in the object that was sent in the jQuery
    
      if (isset($_POST['messageid'])) {
          include 'db_connect.php';
          $usermsg = $_SESSION['username'];
          $message_id = $_POST['messageid'];
          mysql_query("UPDATE messages SET message_read='1' WHERE id='$message_id' AND to_user='$usermsg'");
      }
    
    ?>
    

    Read the Beginners Guide to Using AJAX with jQuery

    And don’t forget to use parametrized sql to prevent sql injection attacks as this code in its current state is vulnurable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been told that it is unsecure to make database connections inside a PHP
I just had a friend of mine look over some code I'm using to
I know that Javascript is an incredibly unsecure way of programming a persistent game,
I'm finding lately how unsecure Javascript is when programming a game (I'm trying to
I just found out you can use <!-- --> brackets in PHP to inject
I have some EXEs for which I do not have code. Can I still
I need to change the user of a PHP script at runtime. I've looked
Does the sleep() function add up for all the user using example.php through an
I have searched the web for how to secure a php session and came
I have two versions of the same proof-of-concept site: The unsecure version: http://www.tlsadmin.com/tlsadmin/PortalHome.aspx and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.