开发者

Call php function through html form

I made a search and checked a few suggestions that this site gave me when writing the headline without success. Here is my problem: On my page I load all links through AJAX and one of the pages is for profiles with data loaded from mysql. At the bottom of the profile-page you can enter a number that stands for time. When doing this I want the page to update the database with entered number and update the current page. Since I use AJAX I just can't add an url I want it to go to or reload the entire page because then I end up at the start-page.

I have made a function called "add_time()" that I want to call for it to calculate and update the database, which I can't load through my form without some tricks that I unfortunate don't know.

I also want to know how I can combine it with my AJAX load_page(url)-script so it makes this two actions at the same time. The divs aint set, but I have marked in the profile-page where the switch is going to be.

The profile page:

<?php
$user = $_COOKIE["user"];
$host = "//";
$username = "//";
$password = "//";
$db_name = "//";
$tbl_name = "user_stats";
mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name") or die("cannot select DB");
$sql = "SELECT * FROM $tbl_name WHERE username='$user'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if ($count == 1) {
    while ($row = mysql_fetch_array($result)) {
        $user = $row[username];
        $level = $row[level];
        $hours = $row[hours];
    }
}
echo "<h1>$user</h1>";
?>

<table id="profile_form">
    <tr>
        <td id="profile_username">Användare: <?php echo $user; ?></td><td id="profile_level"><?php echo "Level: " . $level . "<br />Hours: " . $hours; ?></td>
    </tr>
    <tr>
        <td id="profile_picture">
            Profil picture.
        </td>
    </tr>
    <tr>
        <td id="profile_title">
            Title:
        </td>
    </tr>
</table>
<!-- Div-break. Guess we just call the upper part "profile" and the lower part "add_time" for now.-->
<form action="" method="post">
    Lägg till tid: <input type="text" name="new_time" />
    <input type="hidden" name="prev_time" value="<?php echo $time; ?>" />
    <input type="submit" value="Lägg till tid" />
</form>

The function called from external file:

function add_time() {
    $new_time = $_POST["new_time"] + $_POST["prev_time"];
    $user = $_COOKIE["user"];
    $host = "//";
    $username = "//";
    $password = "//";
    $db_name = "//";
    $tbl_name = "user_stats";
    $con = mysql_connect("$host", "$username", "$password") or die("cannot connect");
    mysql_select_db("$db_name") or die("cannot select DB");
    $sql = my开发者_运维技巧sql_query("UPDATE user_stats SET hours = '$new_time'
WHERE username = '$user'");
    mysql_close($con);
    echo $new_time . "<br />";
}

I hope it helps that I send the code. It's a mess right now, I'm sorry. I think I have dug around so much I might be confusing myself and running around in circles, so I could a second mind that hasn't dug to deep into this rabbits hole.


Jquery is your friend. im not sure i understood ur question but.

u can create an ajax.php file that handle all ur requests include the functions u want and call u upoad $_GET['function'] for example.

ajax.php

<?
if(isset($_GET['function'])){
require_once('update.php');
addtime();
}
?>

that will echo back results for addtime().

jquery

$("#PLACEHOLDER_DIV").load('ajax.php?function=addtime');

tell me what u need in numberd list so i would be able to tell u exact code.

hop it helps. :)


i hope i did understand your problem. if you use jquery you can prevent the default behaviour of your form. instead of submitting your form and going to another page, you can prevent that and submit the data via ajax. i dont know exactly how it works with pure javascript but with jquery its pretty simple.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜