Jump to content

maxxd

Gurus
  • Posts

    1,661
  • Joined

  • Last visited

  • Days Won

    52

Posts posted by maxxd

  1. unfortunately, some versions of browsers (IE/Opera for one) have/had this problem. this can also occur when submitting the form via javascript. the submit button isn't a "successful" form field when the the form is submitted by means other than clicking on the submit button and the submit field isn't included in the form data. in those cases where you need to identify which of multiple possible forms was submitted, using a hidden form field or testing for a field name that is always submitted addresses a type = submit field that may not be sent with the form data.

     

    also, unfortunately, the OP's code in this thread was using name='login' in the form, but was testing for isset($_POST['submit']) in the php code.

     

    Hunh. It works for me in Firefox, Opera, and Chrome in Win and Mac, Safari on Mac and IE on Win. Strange - I'll have to keep an eye to how I test for form submission in the future - thanks for the heads-up!

    • Like 1
  2. First rookie mistake I always see beginners do....

    if(isset($_POST['submit']))
    

    Are you finding these codes online? If you are, the tutorial or page you found it on is probably written from the late 90's - early 2000's. It is time to update. Plus, isset($_POST['submit']) is a hack which will never work ever.

     

    Let's say you have someone who randomly typed up their paper and have every requirement correctly put in, but instead. They hit the "ENTER" key on a <input type="text" name="" value=""> element? What then? The so called "hack" version of checking to see if the submit button has been hit is now broken and the user will be thrown an error simply because the "submit" button has not been hit. Everything they typed up is now lost and their entire 30 page essay has been lost. They have everything correct however, no button was hit.

     

    What then? It's best to use $_SERVER['REQUEST_METHOD'] because it is fool proof and is the actual proper way of checking to see if a form was submitted.

     

    Actually, as long as the submit button has a name property of 'submit', hitting enter instead of clicking the button works just fine. The form as a whole is submitted, so if there's a form element named 'submit' in the form, it gets submitted regardless of submission method (hitting enter on the keyboard or clicking the submit button). The only way I can think that this wouldn't work is if the element named 'submit' is a checkbox that is unselected when the form is actually submitted.

    • Like 1
  3. So where is the code that we've been looking at?

     

    I assumed it was in your functions.php file; you can start the session there just fine without having to call a function on the init hook. Just put the session_start() call at the top of functions.php after the direct access check and everything should work fine across all browsers.

  4. Broke your page how? The die() statements are there to stop execution of the script at that specific point so you can see what's happening. So, in this case, your $_POST and your $_SESSION variables are both set. Is that supposed to happen with this set up? If not, take a step back and rethink the logical flow. If so, then comment out the die() statement, fill out the form again, submit, and keep stepping forward until you find the point where the $_SESSION variables are set in Chrome but not in FireFox.

  5. Try replacing the end of your code with this and let us know what prints:

    //set sessions
    	if(empty($errors) && isset($_POST['submit'])){
    		$_SESSION['dob-month'] = isset($_POST['dob-month']) ? $_POST['dob-month'] : null;
    		$_SESSION['dob-day'] = isset($_POST['dob-day']) ? $_POST['dob-day'] : null;
    		$_SESSION['dob-year'] = isset($_POST['dob-year']) ? $_POST['dob-year'] : null;
    		
    		print("<p>\$errors is empty and \$_POST['submit'] is set</p><p>Session is: </p><pre>".print_r($_SESSION,true)."</pre><p>\$_POST is: </p><pre>".print_r($_POST,true)."</pre>");
    		die();
    		
    //		header("Location: /calculator/",TRUE,303);
    	}
    //set variables and clear sessions
    
    print("<p>Outside conditionals: \$_SESSION is: </p><pre>".print_r($_SESSION,true)."</pre>");
    
    }
    if (isset($_SESSION['dob-month'])||isset($_SESSION['dob-day'])||isset($_SESSION['dob-year'])||isset($_SESSION['submit'])){
    	$month = isset($_SESSION['dob-month']) ? $_SESSION['dob-month'] : null;
    	$day = isset($_SESSION['dob-day']) ? $_SESSION['dob-day'] : null;
    	$year = isset($_SESSION['dob-year']) ? $_SESSION['dob-year'] : null;
    	
    	print("<p>This is the other conditional branch. The date is {$month}-{$day}-{$year}</p>");
    	die();
    	
        session_unset();
        session_destroy();
    }

    Note that your ternaries were malformed, so I updated that. I don't know if that would cause the problem you were having, but it's worth a shot.

     

    Actually, now that I think about it, are you sure this isn't an HTML issue? The php is handled on the server, so if it works on Chrome, it should work on Firefox, IE, Opera, what have you.

  6. I managed to get the form to process correctly by making a few changes and removing the header 303 redirect altogether. The problem I face now is duplicate form request when the page is refreshed because I need to have the output on the same page. Any suggestions?

     

    Typically the way this is done is to do the redirect after the form is processed. Where you're putting the $_POST variables into $_SESSION, redirecting, then processing from $_SESSION, you'll usually do it the other way around. Check to see if $_POST is set, process the data if it is. If the processing is successful, redirect the browser back to the current page. That way $_POST is clear, the form has been processed, and you don't need to muck about with stuffing things into $_SESSION when they're not entirely necessary. I can't remember the term/abbreviation for this method of handling data processing off the top of my head, sorry...

  7. And it's a completely viable strategy; I just typically figure why go through the process of testing and sanitizing the user input to avoid SQL injection when you can use prepared statements and it's not an issue.

     

    That having been said, the other caveat for my method is that you only get that security if you set PDO::ATTR_EMULATE_PREPARES to true, as I believe it's false by default.

  8.  

    It should look something like the following quick sample, although you should apply some validation on each $value and to $_POST['user_id'] before appending it to the query string:

    foreach($_POST['multiBox'] as $record=>$value){
      (!isset($values)) ? $values = "({$_POST['user_id']}, {$value})" : $values .= ", ({$_POST['user_id']}, {$value})";
    }
    $qry = "INSERT INTO user_competency (user_id, comp_id) VALUES ".$values;
     
    //example using PDO:
    $con = new PDO("mysql:host=<yourServerAddress>;dbname=<nameOfDB>","<DBuserName>","<DBpassword>");
    $affectedRows = $con->exec($qry);
    ($affectedRows === 0) ? echo "Either there was nothing to insert, or something went wrong!" : echo "Success! {$affectedRows} were added to the database.";
    

     

    Personally, I'd go a step further and use PDO's prepared statement for added security, like so:

    foreach($_POST['multiBox'] as $val){
    	$tmp['user_id'] = $_POST['user_id'];
    	$tmp['comp_id'] = $val;
    	$vars[] = $tmp;
    }
    $qry = "INSERT INTO user_compentency
    		(user_id, comp_id)
    	VALUES
    		(:user_id, :comp_id)
    	";
    try{
        $sql = $conn->prepare($qry);
        $numRows = 0;
        foreach($vars as $insert){
            $numRows += $sql->execute($insert);
        }
        print("<p>There were {$numRows} inserted into the database!</p>");
    }catch(PDOException $e){
        print("<p>Oops! There was an issue - this is the message: {$e->getMessage()}</p>");
    }
    
    

    Of course, this assumes you're using PDO and have set the error handling to PDO::ERRMODE_EXCEPTION.

     

    Also, note that this is completely untested code and I'm still on my first cup of coffee, so there very well could be a typo or logical glitch in there...

  9. Turn on error checking. You should be getting an error regarding an extra comma at the end of your $insStr.

     

    That having been said, the entire issue could be avoided - and you could have safer and easier database interaction that will still work next year - by moving to PDO or MySQLi. the mysql_* functions have been deprecated for something like a decade now and are slated to be removed from the language with version 7, which I believe drops later this summer.

  10. $formID is passed in to your function from the ninja_forms_display_fields hook. There may be other parameters passed, but the docs only mentioned this one. Try just printing it to see if it's an ID, or an array, or what it is - you may have to use it to get further data from the database.

     

    If you run the code you have above, what's the output? I got the idea that the hook is fired before each field is displayed (not just once), but I could be mistaken about that.

  11. The reply to your post on WordPress forum is probably due to the fact that this isn't the WordPress way of including the jQuery library. Also, check to make sure that WordPress isn't already including it. WP installs a local copy of jQuery in non-conflict mode in order to lessen the chances of conflicts when two different plugin developers use different libraries. You can always dequeue the WP version and enqueue the newer version, but be ware that it may actually break things. Like apparently Huge-it.

     

    WordPress is like that.

     

    Dequeue a script.

    Enqueue a script.

  12. Your theme has a file called functions.php. If you open that up, you'll find all the hooks that the theme uses, and it's here you'd add the call to ninja_forms_display_fields like so:

    add_action('ninja_forms_display_fields', 'thisIsMyFunction', 0, 10);
    function thisIsMyFunction($formID){
    	//do whatever you need to do, using $formID
    }
    

    Again, I'm not sure this is the hook you need, but it's a place to start. Also, if you're using a downloaded or purchased theme you might want to create a child theme out of it before you start messing with the functions.php file as that file runs your entire site.

  13. Hey y'all.

     

    I've got the following code:

    $(window).scroll(function(){
    	if($(window).width() > 640){
    		$('#bg').css({top:$(this).scrollTop()});
    	}
    	if($(window).width() > 981){
    		$('.switch').css({top:$(this).scrollTop() + 75});//75 pixels is the top-offset of the element in the style sheet
    	}
    });
    

    In Firefox, Chrome, Safari, Opera, and Chrome Canary on osX and Firefox, Chrome, and Opera on Windows the div 'bg' stays pinned to the top of the browser window when I scroll. Exactly as intended. Unfortunately, in IE, the div scrolls downwards (against the mouse scroll). But, the 'switch' div stays put - just like it should! It's acting as if the scrollTop() call is returning negative numbers, despite the fact that it's not (I've logged to console and everything looks fine) and it's only doing it in the first conditional. If I change $(this).scrollTop() to a flat 0, it works in IE but nowhere else.

     

    I'm starting to doubt my own sanity. Can anyone come up with a reason I might be seeing this behavior?

  14. Barand, Psycho - thanks so much! Looking at both suggestions together got me to where I needed to be. I ended up with this:

    SELECT * 
    FROM tbl_feature_images
    WHERE (pg_id = :page)
    UNION
    SELECT *
    FROM tbl_feature_images
    	WHERE NOT EXISTS (SELECT *
    			  FROM tbl_feature_images
    			  WHERE pg_id = :page)
    AND pg_id IS NULL
    UNION
    SELECT *
    FROM tbl_feature_images
    WHERE pg_id IS NULL
    	AND display_order NOT IN (SELECT display_order
    				  FROM tbl_feature_images
    				  WHERE pg_id = :page)
    ORDER BY zone, display_order;
    

    which lets my user replace specific images in the rotation on a per-page basis. Y'all rock the hizzy - I would say 'I knew it was something easy', but it wasn't. Thank you much!

  15. Hey y'all. I'm having a bit of brain trouble here in that this seems like it should be really simple, but I'm just ... not coming up with the right query. Any help would be greatly appreciated.

     

    Basically, I've got three featured image 'zones' on each of the pages in my site. Each of the zones has a variable number of absolutely-placed images, stacked one on top of the other. Once the images are in place, my JavaScript takes over and cycles the opacity of each of the images in each of the zones in order from top to bottom, in effect rotating the images. That part I have working nicely, all the way around.

     

    However - and here's where my brain starts to stick - I want the site admin to be able to set site-wide default images for each zone, as well as page-specific images for each zone. So basically, if the page ID is in the featured images table, I want to select those images associated with that page ID. Otherwise, I want to select the images where page ID is null.

     

    I'm embarrassed to say that this is as far as I can get right now on it:

    SELECT	 fi.loc
    	,fi.title
    	,fi.alt
    	,fi.zone
    FROM tbl_featured_images fi
    WHERE fi.pageID = :page
    	OR fi.pageID IS NULL
    ORDER BY fi.zone
    

    This isn't giving me the results I want (for obvious reasons). I feel like the answer's on the tip of my brain, I just can't quite seem to grasp it. Any suggestions?

     

    Many thanks in advance!

×
×
  • 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.