PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hey guys bare with me this is hard to explain...I have a php script included on my main page that posts information from my server. My objective is to make it so the user can enter information in a form on the main page and have the current information displayed switched with the user input and be updated in real time. I used a javascript ajax function which works and passes the form data to the same php script to be processed and show the new data but the php script does not work because when the script is reloaded through jquery it loses the variable $username since it is not being referenced on the main page in which that variable is defined. I thought I could get around that by passing that variable through a hidden form element but I get the message $username not defined even though I want this part to be irrelevant if the form is used and only have the first select statement shown. Therefore the bottom part of my script works initially before the form is used because the information from the database is displayed but I want the new information to be displayed when the form is used. I have this script so far... ANYTHING AT ALL IS HIGHLY APPRECIATED
[color=#FF0000]//this is used so when the form is input the updated information will be shown[/color]
if (isset($_POST['uname']))
{
$fname = htmlspecialchars(trim($_POST['fname']));
$uname = htmlspecialchars(trim($_POST['uname']));
$addClient = "UPDATE fff SET feed='$fname'
WHERE username='$uname'";
mysql_query($addClient) or die(mysql_error());
$name = htmlspecialchars(trim($_POST['uname']));
$first_query="SELECT feed FROM fff WHERE username ='$name'";
$result=mysql_query($first_query) or die('Error, select query failed');
while ($row = mysql_fetch_assoc($result)) {
$thefeed= $row['feed'];
}
}else{
[color=#FF0000]// this is used to show the content initially... this works[/color]
$first_query="SELECT feed FROM fff WHERE username ='$username'";
$result=mysql_query($first_query) or die('Error, select query failed');
while ($row = mysql_fetch_assoc($result)) {
$thefeed= $row['feed'];
}
}
Last edited by scarface222 on Tue May 26, 2009 10:29 pm, edited 2 times in total.
First off, your code seems a little odd - you're doing the same block of code twice, so there's no need for the else statement.
I thought I knew what you were trying to do until I read your code, that threw me off entriely.
From what I understand you're making a form which you want the user to update and once submitted the form will be shown again with the updated data in? This is pretty easy, just have an if-statement at the top of the page checking if the form is submitted (if(!empty($_POST)) normally works fine), then have your select statement below this - so the data is loaded AFTER it's been updated. Then just display your form as normal.
This is what I gathered from your code but your explanation is a bit miss leading. If I got it wrong can you try explaining a little more clearly?
Thank you for the response and your efforts. No you are correct except the bottom portion of the code was to be shown by default before the form is submitted. Once the form is submitted then i wanted the top code to execute. It's confusing because the script is included into the main page where the username variable is defined, but once the form is submitted, since I am using a javascript ajax function shown below the doc process.php becomes seperated and I somehow need to define the username variable so I can successfully update the database. I attempted to do this by passing the variable to the document when the form is submitted which works but then the username variable becomes undefined and the script stops. Does this make more sense? I am sorry I am a rather inexperienced programmer and this is hard to explain. Does this make more sense I did this because in the javascript function I thought only one url could be used so I am attempting to do everything in this one included document?
$(document).ready(function(){
$("form#submit").submit(function() {
// we want to store the values from the form input box, then send via ajax below
var fname = $('#fname').attr('value');
var uname = $('#uname').attr('value');
$.ajax({
type: "POST",
url: "process.php",
data: "fname="+ fname +"& uname="+ uname,
success: function(){
$('form#submit').hide();
$('div.success').fadeIn();
$('#dialog6').load('process.php');
}
});
return false;
});
});
Last edited by Benjamin on Tue May 26, 2009 4:56 pm, edited 1 time in total.
Reason:Changed code type from text to javascript.
session_register();
session_start();
$_SESSION['login_username'] = $username;
then on their page I used
session_start();
$username=mysql_real_escape_string($_SESSION['login_username']);
however when I use the javascript to reload the process.php after the form is submitted the username becomes undefined and when I typed
on the process.php by default it states there is a session running since the page is included on the main page. Is there a way around this? Can I somehow register $username as a global that will be recognized throughout the session. I also tried at the login session_register("username") and that did not work...
Last edited by Benjamin on Tue May 26, 2009 5:59 pm, edited 1 time in total.
Reason:Changed code type from text to php.