TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Javascript, AJAX, E4X (http://www.talkphp.com/javascript-ajax-e4x/)
-   -   Dropdown question... (http://www.talkphp.com/javascript-ajax-e4x/4035-dropdown-question.html)

allworknoplay 03-12-2009 05:31 PM

Dropdown question...
 
Hey guys, I was trying to do this in PHP but I don't think I can.

I think this requires some kind of client end programming.

I'd like to have a drop down with 2 options. The dropdown will have a submit button so no need for any kind of javascript call actions...

Anyways, if you select the first option, it sends you to one page, and if you select the second option, it sends you to another page.

For example:

Option 1: --> This will send you to page1.html via POST method along with any hidden types.

Option 2: --> This will send you to page2.html via POST method along with any hidden types.


TIA!!!

tony 03-13-2009 04:11 AM

I don't think there is only a way doing it with POST data, but it's by using a socket to the domain. however, the sockets won't send the user to the page, it would only send a POST request with its fields.

I don't think it is possible without using javascript.

this code can do the job, but is a vile hack:

PHP Code:

<?php

if($_POST['ur_submit']){
    
$url $_SERVER['HTTP_HOST'].'/'.$_POST['userRequest'];
    
header("Location: ".$urlTRUE307);
    
    
//or instead you can try this
    /**
    $post_data = 'var1=123&var2=456';
    $content_length = strlen($post_data);
    $host = 'your.host.com';
    
    header('POST '.$_POST['userRequest'].' HTTP/1.1');
    header('Host: '.$host);
    header('Connection: close');
    header('Content-type: application/x-www-form-urlencoded');
    header('Content-length: ' . $content_length);
    header('');
    header($post_data);
    
    exit();
    */
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <select name="userRequest" id="userRequest">
        <option value="page1.html">Option1</option>
        <option value="page2.html">Option2</option>
    </select>
    
    <input type="submit" name="ur_submit" id="ur_submit" value"Submit" />
</form>

In the code there are 2 ways to do it, the first one is the code
PHP Code:

header("Location: ".$urlTRUE307); 

This is basically a hack because it messes up with the search engines and they won't index the file that has this code because it would have this redirect header. Also the web browser would ask the user if he truly wants to redirect, except for one (IE) that doesn't implement the right standards.

The second way (that is commented out) is just an imaginary code from that some people says it works, I am not sure, I haven't test it.

Another way to do it is using javascript kinda like this:

Code:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title><!-- Insert your title here --></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<style type="text/css">
form#redirect{
    display: none;
}
</style>

<script type="text/javascript">
window.onload=function(){
    var form=getElementById("redirect");
    form.submit();
}
</script>

</head>
<body>
<?php
if($_POST['ur_submit']){
    $redirect = $_POST['userRequest'];
?>

<form name="redirect" id="redirect" action="<?php echo redirect; ?>">
    <input type="hidden" name="var1" value="123" />
    <input type="hidden" name="var2" value="456" />
</form>

<?php
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <select name="userRequest" id="userRequest">
        <option value="page1.html">Option1</option>
        <option value="page2.html">Option2</option>
    </select>
   
    <input type="submit" name="ur_submit" id="ur_submit" value"Submit" />
</form>


</body>
</html>

I am not good at JavaScript either.
The problem with not being able to send post requests without submitting forms in php is because it runs on the server and the post requests are send on the client side, so we need JavaScript to send a post too, as far as I know.

I hope this helps.

allworknoplay 03-13-2009 05:08 PM

Thanks Tony,

I will take a look at your code and see what I could possibly use.

I was actually looking for a javascript. Hoping that there was some kind of javascript/ajax way of doing it. I know that this is possible somehow because I've seen it at various sites. When I look at my post, I don't think I was entirely clear of what I wanted to do so I will try to add more to what I wrote.

The login page has username and password as the input fields
with a submit button.

Basically the main page, let's call it: index.html is the login page. There's a drop down with only 2 options.

You can put in your username/password combo and whichever dropdown you select takes you to either page1.html or page2.html.

By default, the first option goes to page1.html and obviously if you choose the 2nd option, it takes you to page2.html...



So somehow based on what you select in the dropdown box, your typical "form" tag will change the "action" page that it will go to. I just can't seem to figure that part out since it seems it needs to be dynamic and I'm not very good at javascript...

I will try out your code though, hey, if it works, then it works...and I will be eternally greatfull!

tony 03-13-2009 09:09 PM

Oh, my bad, I thought you said no javascript.
I've also since this behavior around, but it's been a while (in the Web 1.0 era).
with ajax is the same thing as using curl, you can send the post data but it won't redirect you to the page you send the post data, as far as I know. I think the most viable is having another form besides the one the user needs to select the option and send that with the post data that you want.

allworknoplay 03-13-2009 11:20 PM

Quote:

Originally Posted by tony (Post 22222)
Oh, my bad, I thought you said no javascript.
I've also since this behavior around, but it's been a while (in the Web 1.0 era).
with ajax is the same thing as using curl, you can send the post data but it won't redirect you to the page you send the post data, as far as I know. I think the most viable is having another form besides the one the user needs to select the option and send that with the post data that you want.

I'm kinda from the Web 1.0 world. I mean, I've been developing sites the last 10 years but I never really tried learning the new stuff..I just got by with what I learned back in the days and it was always good enough for my projects.

Now I see things getting much more dynamic and I'm trying to keep up. Shame on me for "living" off what I knew and never really learning more. But at my job, I'm kinda the jack of all trades (master of none) so it's very difficult for me to keep up when I'm also the Cisco guy, Firewall guy, Linux server guy etc....

I'm going to look into CURL, that COULD be what I'm looking for....

I think I'm pretty close to figuring this out, when I do, I will post my solution...thanks for your helpful tips!!

tony 03-14-2009 03:09 AM

I also learned the web 1.0 knowledge, just about 2 years ago I started learned up all the new stuff that was happening. I understand how overwhelming all this is getting. But man Cisco services is in itself a lot of work.
kudos man, that you are trying to figure all this out besides being the go to guy.

About your problem, are you trying to have a login form with a password textbox, username textbox, and the combobox to select where would the username and password input would go to be processed. After this do you want to send the user back to the index (or main page) already logged in if the loggin was successful?

If that's the case it can be done with curl or an ajax request.

allworknoplay 03-14-2009 05:13 PM

Quote:

Originally Posted by tony (Post 22224)
I also learned the web 1.0 knowledge, just about 2 years ago I started learned up all the new stuff that was happening. I understand how overwhelming all this is getting. But man Cisco services is in itself a lot of work.
kudos man, that you are trying to figure all this out besides being the go to guy.

About your problem, are you trying to have a login form with a password textbox, username textbox, and the combobox to select where would the username and password input would go to be processed. After this do you want to send the user back to the index (or main page) already logged in if the loggin was successful?

If that's the case it can be done with curl or an ajax request.


Yeah Cisco is a handful....let's not even get into that! It is fun though!

Anyways, yeah, I think what you described is what I am attempting to do.

To make it simple, let's just say that the 2 options would go to 2 different websites with the username/password posted to them.

So say the two options were: AOL.com and TALKPHP.com

I would select one of the two, put in the username/password, hit submit, and it would then post my username/password post data to the selected site, and the selected site would take care of logging me in...

I thought that I could get clever and have a "switch.php" file that would use the PHP "header function" to redirect me to either site based on what I selected, but the problem is that I lose the $_POST[username] and $_POST[password] in the process of the redirection...

If only I'm able to hold onto the data and pass it forward then all is dandy....

allworknoplay 03-14-2009 05:49 PM

I think I'm close, I found something like this with CURL but I'm getting an error...

Code:

<?php
 $ch = curl_init('index.html');
 curl_setopt ($ch, CURLOPT_POST, 1);
 curl_setopt ($ch, CURLOPT_POSTFIELDS, "email_address=email&password=password");
 curl_exec ($ch);
 curl_close ($ch);
?>


Fatal error: Call to undefined function: curl_init() in /usr/local/apache/test.html on line 2

I know that I have curl installed so I'll have to dig into this error further....

tony 03-14-2009 10:38 PM

Maybe the curl module is not enable in the php config file.

This is a good website with examples of curl


All times are GMT. The time now is 03:29 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0