最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Run php code in a script function when the function is called - Stack Overflow

programmeradmin1浏览0评论

I have a file named a.php which has a html button with a script function within the same page.Code is given bellow

    //a.php

    <input name="wpost" type="button" value="Publish" onClick="wall_publish()"/>

    <script type="text/javascript">
    function wall_publish(){
    //some codes here..
    //php database part  
        <?php       
            $host = 'localhost';
            $username = 'root';
            $password = '';
            $database = 'uploader';

            $con = mysql_connect($host,$username,$password);
            if (!$con){
               die('Could not connect: ' . mysql_error());
            }
            mysql_select_db($database, $con);   
                mysql_query("UPDATE  image_uploader SET Tag_Count = '$tag_count', Tag_List = '$tag_list' WHERE Image_Link = '$source'");
                mysql_close($con);
        ?>
    }
    </script>

I want to execute the php database part when the user click the "Publish" button(Not when the page is loaded).,
Is there a way to do this?
Just placing this php code inside the script function doesn't work.It is executed when the page is loaded.

I have a file named a.php which has a html button with a script function within the same page.Code is given bellow

    //a.php

    <input name="wpost" type="button" value="Publish" onClick="wall_publish()"/>

    <script type="text/javascript">
    function wall_publish(){
    //some codes here..
    //php database part  
        <?php       
            $host = 'localhost';
            $username = 'root';
            $password = '';
            $database = 'uploader';

            $con = mysql_connect($host,$username,$password);
            if (!$con){
               die('Could not connect: ' . mysql_error());
            }
            mysql_select_db($database, $con);   
                mysql_query("UPDATE  image_uploader SET Tag_Count = '$tag_count', Tag_List = '$tag_list' WHERE Image_Link = '$source'");
                mysql_close($con);
        ?>
    }
    </script>

I want to execute the php database part when the user click the "Publish" button(Not when the page is loaded).,
Is there a way to do this?
Just placing this php code inside the script function doesn't work.It is executed when the page is loaded.

Share Improve this question asked Aug 8, 2011 at 6:15 RoshanckRoshanck 2,2909 gold badges43 silver badges56 bronze badges 3
  • 2 :D You are mixing client side (JavaScript) with server side (PHP) languages. – Shef Commented Aug 8, 2011 at 6:17
  • 1 You should look into AJAX: developer.mozilla/en/ajax – Paul Commented Aug 8, 2011 at 6:18
  • User ajax....it is used to call server side script using javascript – Sumair Zafar Commented Aug 8, 2011 at 9:34
Add a ment  | 

4 Answers 4

Reset to default 3

If you want to call PHP code from javascript, the short answer is: you can't. PHP is server side code, and javascript is client side code. They run on different puters.

What you can do, however, is using javascript to send a HTTP request to the server - AJAX. You can then execute the code with the information the javascript has sent with the HTTP request.

Here is a short ajax tutorial from tizag that might get you started:

http://www.tizag./ajaxTutorial/

You will have to put the PHP code into a seperate file and make the form action point to that script. Then, when the user clicks the button it will fetch the file and execute the script on the server. If you do not want a page replacement to happen, instead of making a traditional post form, you can run an ajax request upon clicking the button. This will prevent a page replacement.

First you should read some tutorials on PHP and JavaScript, and not just the programming part, but rather the theory part. JavaScript is executed on the client, PHP on the server.

The solution is for JS to make an AJAX request to the server.

<input name="wpost" type="button"id="my_button" value="Publish" />

put this javascript into the head section of your a.php file.

<script type="text/javascript">
$("#my_button").click(function() {
  $.post("page.php");
});
</script>

don't forget to include jquery library file as I jquery is used for the solution.

in page.php u should write:

    <?php       
        $host = 'localhost';
        $username = 'root';
        $password = '';
        $database = 'uploader';

        $con = mysql_connect($host,$username,$password);
        if (!$con){
           die('Could not connect: ' . mysql_error());
        }
        mysql_select_db($database, $con);   
            mysql_query("UPDATE  image_uploader SET Tag_Count = '$tag_count', Tag_List = '$tag_list' WHERE Image_Link = '$source'");
            mysql_close($con);
    ?>

notice you did not assign values for $tag_count, $tag_list.

Don't u want to show any success or failure message after the sql query operation?

发布评论

评论列表(0)

  1. 暂无评论