Jump to content

7blake

Members
  • Posts

    32
  • Joined

  • Last visited

Everything posted by 7blake

  1. That doesn't seem to work without defining $userAnswer as such. .. because I guess there's nothing in there display. But I got it to work on that point, thanks $test = $_SESSION['test'][$userAnswer] = "string"; || $test = $_SESSION['test'][$userAnswer] = array("one", "two"); Bah. I have such poor terminology. Sorry, I'm not even sure how to google this. What is the correct term for ['test']/[$userAnswer]? Are they indexes? Time to read up on arrays again I still am unsure as to why the IF statement is not fireing. Any ideas?
  2. Hmm. There's a problem somewhere prior to that in my code. Or I am doing this structurally wrong. When I click the button, ajax sends via POST 'name'(not sure what you call this, POST variable?) Then PHP recieves it, checks if it isset, and moves to the IF statement. On the first click $_SESSION['test'] is not set, so the else is executed. But in the else, am I not initiating $_SESSION['test'] ? I want it so when I click the button a second time, it runs the IF statement. Is the problem with my ajax? Or maybe my SESSION isn't starting properly? I tried your suggestion with echo json_encode($_SESSION['test'][$username]) and that won't pass any information. <?php if(isset($_POST['name'])) { $userAnswer = $_POST['name']; if(isset($_SESSION['test'])){ $_SESSION['test'][$userAnswer]['var1']++; $test = $_SESSION['test'][$userAnswer]; echo json_encode("test2"); } else { $test = $_SESSION['test'][$userAnswer]=array("var1" => 1, "var2" => 2); echo json_encode("test"); } } ?>
  3. Ahh So that'd be called using your head. Cool thanks. (btw - how come I don't have to use a loop to access the array? Is it converted to a string?) *edit* Ah ok nm Do you by chance know what is wrong with my first code? Is it my SESSION itself not registering or how I've written it out?
  4. Maybe trying simplifiying the application down to one input field just so you can understand how it works. That way, once you understand what is happening, you can add more fields and make it more complex. If you understand your foundations, it's easier to spot where the issues are. Just a suggestion. gl
  5. Like so? <?php if(isset($_POST['name']) && isset($_POST['name2'])) { $userAnswer = $_POST['name']; // name == 1 $userAnswer2 = $_POST['name2']; // name == 2 $newArray = array(); $newArray[0] = $userAnswer; $newArray[1] = $userAnswer2; echo json_encode($newArray); } ?> I suppose that works. I'm a bit confused actually as to why it's being made into a string? Does json_encode convert arrays to string? I thought I needed a foreach to echo the array. But in anycase, this wasn't the concept I was trying to understand.(but it's produced more questions .. :|) In the inital example, $_SESSION['test'] is not Set. So the "else" statement runs. But when I initate the action again, shouldn't $_SESSION['test'] be set, thus executing the "if" statement instead of the "else" statement?
  6. It's there, I just didn't want to put in all the code because it's rather long. (sorry, should have cleared that up in the inital post) <?php session_start(); ?> <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script> <script src="//code.jquery.com/jquery-1.9.1.js"></script> <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <script src="jquery.ui.touch-punch.min.js"></script> <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/base/jquery-ui.css"/> <script src="jquery.custom.js" > </script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ var test = "here"; $.ajax({ url: 'info.php', type: "POST", dataType:'json', data: ({name: test}), success: function(data){ $("#click").fadeOut(function() { $(this).append(data); }).fadeIn(); } }); }); }); </script>
  7. Hi, I'm very new to PHP so I hope this is not frustrating. I'm having trouble understanding how "isset" works. I'm trying to get it so that when I click a button, it returns two variables(in an array). This works, but I tried to use "Isset" to detect if a array was created, then alter the variable. It's not working... I'm not too sure where I'm going wrong, could anyone give me some advice? Thanks <?php if(isset($_POST['name'])) { $userAnswer = $_POST['name']; if(isset($_SESSION['test'])){ $_SESSION['test'][$userAnswer]['var1']++; $test = $_SESSION['test'][$userAnswer]; foreach ($test as $key){ echo json_encode($key); }; } else { $test = $_SESSION['test'][$userAnswer]=array("var1" => 1, "var2" => 2); foreach ($test as $key){ echo json_encode($key); }; } } ?> $(document).ready(function(){ $("button").click(function(){ var test = "here"; $.ajax({ url: 'info.php', type: "POST", dataType:'json', data: ({name: test}), success: function(data){ $("#click").fadeOut(function() { $(this).append(data); }).fadeIn(); } }); }); });
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.