Jump to content

[SOLVED] php new window question


Mattio2187

Recommended Posts

I have given up on using ajax, as my deadline is nearing a close and I can't figure it out. Instead I have a question.

I am running a form with a few radio buttons. The php itself runs a cookie and ip check and either:

- inserts the results into a database and says "Thank You"

or

-returns to user "You have already voted. Good Day!"

 

However, this response replaces the current page, which is expected with a basic call to a php page.

 

I would instead like to to have the php response open in a new window instead of replacing the current page. Can I do it while still inserting to my database?

 

Any help would be greatly appreciated.

 

The current code is live a mattsolano.com/zink/voting.html if your interested

 

thanks

Link to comment
Share on other sites

either u use Javascript to open the result in a new window, or set the target param of FORM element as _BLANK.

 

 

Will this still run my php code that is in a separate file? What exactly do you mean by target, do you mean action or method?

 

 

I hate to be a pain but could you display a short and quick example. I think i've done this for html links

Link to comment
Share on other sites

By Target, I mean target, not action or method .. there is a param for FORM element called TARGET.

<form name="myForm" action="newwindow.php" method="post" target="_blank">

<!-- FORM ELEMENTS -->

</form>

 

Now wat will happen, whenever user click on submit, it will open the page newwindow.php in a new window, and send all the form inputs (radio, text anything) to the new page as POST variables. Now you write your PHP code (inserting into db, showing result etc etc watever u want) in the new page newwindow.php

 

This new windoe will be a regular window i.e. having all the menubars. If you want the new window a POP-UP window (having no menubars), you have to use slightly javascript.

Link to comment
Share on other sites

Ok, thats what I figured. Thank you, but I did try this and its still doing the same thing.  I attempted to incorporate the java code, as I have done it before, but unfortunately it stopped placing the entries into the database (which kind of defeats the purpose).

 

I may try submitting that code to the javascript forum, unless you or anyone else has any other ideas.

 

I think my issue is with this ip/cookie check that i used. It's making everything more difficult than it has to be.

 

Here's the code for anyone curious:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<style type="text/css">

.selectbox {
width:	100px;
height:	100px;
border:	1px solid red;
float:	left;
margin:	20px;
}

</style>


<body>

<form action="vote.php" method="post" target="_blank">
<div class="selectbox">Choice 1: <input type="radio" name="picked" value="one"/></div>
<div class="selectbox">Choice 2: <input type="radio" name="picked" value="two"/></div>
<div class="selectbox">Choice 3: <input type="radio" name="picked" value="three"/></div>
<input type="submit" />
</form>

</body>
</html>



PHP Page:



<?php


$con = mysql_connect("localhost","mattsol1_user","user");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("mattsol1_pollthree", $con);

$theVote = $_POST[picked];

if(isset($_COOKIE['ip']) && !empty($_COOKIE['ip'])) { 
     if(isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR'])) { 
          if($_COOKIE['ip'] == $_SERVER['REMOTE_ADDR']) { 
               $ipAddr = $_COOKIE['ip']; 
          } else { 
               setcookie("ip", FALSE); 
               setcookie("ip", $_SERVER['REMOTE_ADDR']); 
               $ipAddr = $_SERVER['REMOTE_ADDR']; 
               die("It appears your IP has changed since you last voted or you played around with your cookies. Either way, you're getting a new cookie. If you don't know why this is happening, do not worry - you did nothing wrong. <a href='path/to/your/poll/mainpage/'>Return home</a>"); 
          } 
     } else { 
          die("It appears I cannot grab your IP address and thus cannot tell if you voted already."); 
     } 
} else { 
     $ipAddr = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']); 
     setcookie("ip", $ipAddr); 
} 

$ipCheck = mysql_query("SELECT * FROM `vote` WHERE `ip` = '" . mysql_real_escape_string($ipAddr) . "'"); 
if(mysql_num_rows($ipCheck) > 0) { 
     die("You have already voted in this poll. Good day!"); 
} else { 
     $sql= "INSERT INTO `vote` (`ip`, `picked`) VALUES ('$ipAddr', '$theVote')"; 
}

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Thank You";

mysql_close($con);

?>

 

Please use code tags next time -zanus

 

Link to comment
Share on other sites

I dont find anything wrong in your coding. Can you echo $ipAddr just before the following line to check whether you can grab the IP succefully

$ipCheck = mysql_query("SELECT * FROM `vote` WHERE `ip` = '" . mysql_real_escape_string($ipAddr) . "'");

Link to comment
Share on other sites

I know there isn't anything wrong with the code I have here. I just figured if anyone wanted to take a look at what I was working with it. 

 

Another quick question.

Would I be able to place the php code into the bottom of the html so that the form references the same page? The page wouldn't change, would it?

 

If I could do that then I could maybe figure out little jquery script that displays a message or even just an alert.

Link to comment
Share on other sites

ofcourse you can ... if you have no objection on having the response in the same page, you can put your php code anywhere in the page (i prefer to put php code on top), then just echo the message (you want to give the visitor) in the appropriate place. But one thing u remember, as you want to execute your php code only when the visitor submit the button (as I understand), you should enclose all your php code inside an if statement, like below .

 

<form action="vote.php" method="post" target="_blank">
<div class="selectbox">Choice 1: <input type="radio" name="picked" value="one"/></div>
<div class="selectbox">Choice 2: <input type="radio" name="picked" value="two"/></div>
<div class="selectbox">Choice 3: <input type="radio" name="picked" value="three"/></div>
<input type="submit" name="submit_me" />
</form>

<?php
if(isset($_POST[submit_me])){

// YOUR PHP CODE LIES HERE

[color=red]}[/color]
?>

Link to comment
Share on other sites

Well Thank you for that. The message at least now opens in a new, so I don't know what you did there but thanks. The only issue I can's sort out is the fact that the php code now displays on the actually webpage in plain text. I'm not sure where to put it so that it doesn't display.

 

you can see what I'm talking about at mattsolano.com/zink/voting.html

 

I really appreciate you helping me out

Link to comment
Share on other sites

this is obvious that you made some PHP pursing error. You better paste your whole code here, and i'll make the correction.

 

Probably, but no worries. I just removed the php code as I found the form wasn't even refering to it, but still the original php doc. Somehow the version of the form that you gave me allowed the _BLANK to finally work, so i'm satisfied. Especially since I have to start incorporating this voting function into the actual design. There are 26 submissions to vote on and each radio button is going to have an image and small description above it. Hence my red box divs. they'll be arranged in large grid on the page. Hopefully it'll turn out as I imagine.

 

Thanks again

Link to comment
Share on other sites

you are welcome. A last tip, if i were you, i would definitely wanted my new window to be a pop-up. To make your new window a popup window, you can replace your FORM TAG line with the following

<form name="myForm" action="vote.php" method="post" target="mynewwindow" onSubmit="window.open('', 'mynewwindow', 'width=450,height=300,status=yes,resizable=yes,scrollbars=yes')">

 

and btw, fyi, in the previous post I just added a name to your submit button,that's it. :)

 

cheers, good luck for your project.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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