Jump to content

Extremely basic question?

- - - - -

  • Please log in to reply
22 replies to this topic

#1
12rmcmillin

12rmcmillin

    Newbie

  • Members
  • PipPip
  • 28 posts
Hey there. I was just trying to learn a little extra php, and had an idea. It's nothing major at all, but it has a few potential uses. I'd like to take input from a text box, and put it on the end of an already defined link. Any help? For example: They type in 50152 and hit submit. The link displayed would be "http://www.website.com/50152

If possible, that would be a link. How would I go about doing this? I'm just starting to learn php. Thanks guys.<3

Edited by 12rmcmillin, 06 October 2011 - 04:53 PM.


#2
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
You could do this with php.
Submit the form, and via php redirect

But the easy way would be using javascript.
<input type="text" id="myId" />

<a href="#" onClick="window.location = 'http://www.test.com/' + document.getElementById('myId').value; return false;">Click Me!</a>

Of course, you should validate if myId has a value.

#3
12rmcmillin

12rmcmillin

    Newbie

  • Members
  • PipPip
  • 28 posts
I'm looking for a way to do it with PHP, just so I can know that little bit more about it.

#4
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
ok than you'll 2 page

page1.php

<form action="page2.php" method="post">

<input type="text" name="myName" />

<input type="submit" />

</form>


page2.php

<?php

if (isset($_POST['myName'])) {

header('Location: http://www.example.com/' . $_POST['myName']);

}

else {

header('Location: page1.php');

}

?>


This is the easiest way to do so

#5
12rmcmillin

12rmcmillin

    Newbie

  • Members
  • PipPip
  • 28 posts
EDIT: I decided to just display the full link instead of trying to make it go to the link automatically, just to see if it would display the entire link. I found another use for it I'm trying right now, to just display the link. I'll post back when I finish or have any other problems.

#6
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
Because the function header send data into the header of the page.
So it has to be the first thing to be sended, if you send something else first, it will not work.
Don't echo/print anything before this, even whitespace

#7
12rmcmillin

12rmcmillin

    Newbie

  • Members
  • PipPip
  • 28 posts

Vaielab said:

Because the function header send data into the header of the page.
So it has to be the first thing to be sended, if you send something else first, it will not work.
Don't echo/print anything before this, even whitespace

Changed to two inputs for a little idea I had, got the final output to function exactly as I wanted. Now I'd just like to either make this a link, or automatically go to the page the link is generated for. And I know I only used the if statement for the "Band" input. Too lazy to make everything perfect right now.

<?php

if (isset($_POST['band'])) {

echo ('http://www.google.com/#sclient=psy-ab&hl=en&safe=active&source=hp&q=' . $_POST['band'] . '+' . $_POST['album'] . '+zip+mediafire');


}

else {

echo ('Error: form empty.');

}

?>


#8
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
Just need a little update

<?php

if (isset($_POST['band'])) {

echo ('<a href="http://www.google.com/#sclient=psy-ab&hl=en&safe=active&source=hp&q=' . $_POST['band'] . '+' . $_POST['album'] . '+zip+mediafire">My link</a>');


}

else {

echo ('Error: form empty.');

}

?>



#9
12rmcmillin

12rmcmillin

    Newbie

  • Members
  • PipPip
  • 28 posts

Vaielab said:

Just need a little update

<?php

if (isset($_POST['band'])) {

echo ('<a onclick="_gaq.push(['_trackEvent', 'Outgoing', 'www.google.com', '/']);" rel="nofollow" href="http://www.google.com/#sclient=psy-ab&hl=en&safe=active&source=hp&q=' . $_POST['band'] . '+' . $_POST['album'] . '+zip+mediafire">My link</a>');


}

else {

echo ('Error: form empty.');

}

?>


Works like a charm. Any way I could make it go to the final search link directly after submitting the form, using the modified search link?

#10
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
TO have your _gap.push, you will have to have some more javascript and do the redirection with javascript
You'll need to do something like this.

Put this in your
<head>

<?php

$script = <<<EOD

<script type="text/javascript">

function load() {

  _gaq.push(['_trackEvent', 'Outgoing', 'www.google.com', '/']);

  window.location = "http://www.google.com/#sclient=psy-ab&hl=en&safe=active&source=hp&q={$_POST['band']}+{$_POST['album']}+zip+mediafire";

}

</script>

EOD

;

echo($script);


?>


And on your body
<body onLoad="load();">

I didn't test it, so can't assure you this will work, but it should look like something like that

#11
12rmcmillin

12rmcmillin

    Newbie

  • Members
  • PipPip
  • 28 posts

Vaielab said:

TO have your _gap.push, you will have to have some more javascript and do the redirection with javascript
You'll need to do something like this.

Put this in your
<head>

<?php

$script = <<<EOD

<script type="text/javascript">

function load() {

  _gaq.push(['_trackEvent', 'Outgoing', 'www.google.com', '/']);

  window.location = "http://www.google.com/#sclient=psy-ab&hl=en&safe=active&source=hp&q={$_POST['band']}+{$_POST['album']}+zip+mediafire";

}

</script>

EOD

;

echo($script);


?>


And on your body
<body onLoad="load();">

I didn't test it, so can't assure you this will work, but it should look like something like that

gap.push isn't anywhere in my original code.. Should it be?

#12
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
You added it when you quote my answer at 02:05 PM.
gap.push is for google analytics, maybe it was the forum that add it...




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users