1

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 = mysql_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.

2 Answers 2

1

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. :)

Sign up to request clarification or add additional context in comments.

2 Comments

I have the ajax code for loading my page. So here is the list: 1. Call a php function from form, or something that works in the same way. 2. Updating the page without it returning to start. And I would be very glad if I can do this without it changing the url. A little experiment I'm making, how much I can do without having to do that.
return false; is key player here. $('a').click(function(){$("#PLACEHOLDER_DIV").load('ajax.php?function=addtime');return false;});
1

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.

3 Comments

$('form').submit(function(e){ e.preventDefault(); //do something });
Submit data through AJAX? How do you mean? I do so with my links, but otherwise I don't know how to do it in a smooth way. Care to evaluate that? :)
here is a tutorial on how to prevent the default behavior of a form: blog.james-carr.org/2008/01/17/… and here is explained how POST with AJAX works : api.jquery.com/jQuery.post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.