Jump to content

How to use query strings in HTML?


itsjareds

Recommended Posts

Hi, I need to write a page that uses queries in the url like

example.com/example.php?thisiswhatiwant=2

If there is no question mark, I want it to display an alert box asking "What would you like this variable to be?" If there is something after the question mark, I want it to add what the variable is here:

<PARAM value="[color=red]query_variable[/color]">

 

I tried to do something similar with javascript that worked like this:

<head>
<script type="text/javascript">
function getName()
{
  var name = prompt("What is the title name of your picture?","Enter name here");
  if (name!=null && name!="")
  {
  return name;
  }
}
</script>
</head>
<body>
<applet>
<PARAM value="<script type="text/javascript">document.write("getName()");</script>">
</applet>
</body>

 

I've written in PHP but it was so long ago, and I can't remember anything.

 

Any help?

 

 

Link to comment
https://forums.phpfreaks.com/topic/110360-how-to-use-query-strings-in-html/
Share on other sites

I don't really have enough info to answer this question all the way but what I would do is is pass the variable through the URL regardless then just check to see if it has been set :

 

"http:www.url.com?variable=xxx"

 

<?php

if (!isset($_GET['variable'])) {
  //whatever you want to happen if the variable isn't set.
} else {
  //whatever you want to happen if the variable IS set such as checking for certain values.
}

?>

 

hope that helps . . . sorry if it doesn't.

Ok, that helped some, so I tried writing with your code. This is what I have so far:

 

Notice that I'm not sure what needs to be escaped, so I played it safe..

<?php

if (!isset($_GET['pic'])) {
  echo "<script type='text/javascript'> function getName() \{ 
var name = prompt(\'What is the title name of your picture\?\',\'Enter name here\')\;
if (name!=null && name!=\'\')\{
window.location.href = window.location.href + \'?pic=\' + pic\;
\}
\}";

?>

<head>
</head>
<body>
<applet>
<param ... >
<?php echo "<PARAM NAME='GALLERY_PIC' VALUE='". $pic ."'>"; ?>
</applet>
</body>

 

I get an error that says Parse error: syntax error, unexpected $end in /home/myhomepage on line 62. The problem is I can't find any missed closing tags or anything ???

Woo! Nevermind, I figured it out ;D

 

Here's my code

 

<head>
<?php

if (!isset($_GET['pic'])) {
  echo "<script type=\"text/javascript\"> 
var name = prompt('What is the title name of your picture?','Enter name here');
if (name!=null && name!='')
  {
  window.location.href = window.location.href + \"?pic=\" + name;
  }
else 
  {
  name = \"invalid\";
  window.location.href = window.location.href + \"?pic=\" + name;
  }
</script>";
}

?>
</head>
<body>
<applet>
<param .... >
<?php echo "<PARAM VALUE='". $pic ."'>"; ?>
</applet>
</body>

 

Lots of php coming back to me now :D

Archived

This topic is now archived and is closed to further replies.

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