0

The header redirect after form submit isn't working and I'd love some help here please (everything else is working fine)...

            <?php
            require_once "twoauth/autoload.php";
            use Abraham\TwitterOAuth\TwitterOAuth; 
            require("twconfig.php");     
            $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
            
            $status = $_POST['tweetText']; 
            $filepath = $_POST['uploadedFile'];
            $media1 = $connection->upload('media/upload', ['media' => $filepath]);
            $parameters = [
                'status' => $status,
                'media_ids' => implode(',', [$media1->media_id_string])
            ];

            if(isset($_POST['submitTweet'])) {
                $post_tweets = $connection->post('statuses/update', $parameters);
                header('Location: nextpage.php');
                exit();
            }
        ?>  

Additional Info: There aren't any errors and it doesn't crash. It just sends off the data and reloads the current form/page. It's a regular form submission, not AJAX. I also tried an inline javascript onsubmit window.location.href call, but that didn't work either. I had a look at the HTTP request/response and cannot see the word "Location" at all. There is "URL" in the summary section and "Referrer" in the request section, but that's it as far as location/URLs go.

Matt
  • 1
  • 1
  • we need a bit more precision. When you say "isn't working", you mean what exactly? Does the code crash at/before the point where the header command is executed? If so, what's the error? Or do you get any other kind of warning from PHP? Have you checked the HTTP response (e.g. using your browser's Network tool to examine the HTTP request/response) to this request to see if the "Location" header is actually present? (Also, can you please confirm that you're doing a regular form submission from the browser, and _not_ an AJAX request?). Any other info you can give us about the precise behaviour? – ADyson Sep 06 '20 at 21:46
  • There aren't any errors and it doesn't crash. It just sends off the data and reloads the current form/page. – Matt Sep 06 '20 at 22:09
  • Yes, it's a regular form submission. I also tried an inline javascript onsubmit window.location.href call, but that didn't work either.... I had a look at the HTTP request/response and cannot see the word "Location" at all. There is "URL" in the summary section and "Referrer" in the request section, but that's it as far as location/URLs go. – Matt Sep 06 '20 at 22:26
  • Ok. So the next obvious possibility...are you sure that the PHP code definitely enters the `if(isset($_POST['submitTweet'])) {` block? – ADyson Sep 06 '20 at 22:28
  • Well, I'm not completely sure what you mean, but that bit of code is what posts the form data to Twitter, so that part of it is definitely working. – Matt Sep 06 '20 at 22:40
  • There are two lines which appear to send things to Twitter. Are they definitely both working? What I meant was, have you debugged it to see if the code is definitely going inside the `if` block? I.e. to check that the `if` test returns true? Because if it doesn't, then it would skip issuing the Location header - but it would also skip the second upload to Twitter – ADyson Sep 07 '20 at 06:28

0 Answers0