As per requirement, I want to allow the user to write to session when submitting a form but without reloading the page, this is what i have done so far:
Javascript function:
$(function() {
$("#body").submit(function(){
var name = $("#player_name").val();
var dataString = 'player_name='+ name
alert (dataString);return false;
$.ajax({
type: "POST",
url: "session_update.php",
data: dataString,
success: function(){
$('#body').html("<div id='message'></div>");
$('#message').html("<h2>Logged in!</h2>");
});
}
});
return false;
});
My form im trying to submit:
<div class="body">
<div class="home" id="home" style="width:100%">
<form action="" name="login">
USERNAME: <input type="text" id="player_name" name="player_name"/>
<br>
<input type="submit" name="submit" class="button" id="submit_btn" value="Send" />
</form>
<? echo "value=". $_SESSION['player_name'];?>
</div>
the php file its sending the data to:
<?php
start_session();
$_SESSION['player_name']= $_POST["player_name"];
?>
any advice your be greatly appreciated, fairly new to this side of coding so it could be something obvious thats causing it but i just cant see it.
For more information let me know.
UPDATE
Console showing
?player_name=User&send:75
You need to use
session_start();notstart_session()Always ensure that you test your PHP scripts manually, not just pointing to the page with ajax. Go to it and make sure it works. 🙂
If you tested this (assuming error_reporting is on), you will have received an error stating that you are trying to call an undefined function.
UPDATE
In addition to the
session_start()issue, you have may other problems in your code.First step is to debug using either
console.log('hello');oralert('hello'). Bind these actions to the form submit event and this will tell you whether or not the submit function is being called.Once you have done this then you can continue to debug the rest 🙂
UPDATE 2
From looking at your jQuery, it would appear that you are attaching the
submitevent to the wrong element in the DOM. You need to attach it to the form, not a div. Like so:and
TIP: Use CSS to space elements, don’t use
brtags. Just my opinion 🙂