Jump to content

[SOLVED] Do You Know How to do This . . . ?


Recommended Posts

Anyone know how to ask a simple question with a yes or no answer, then, whatever's picked, have the date on which it was picked be displayed? For example:

 

Did this tip work for you?

Yes | No

Last worked: 21st October, 2008

 

Or:

 

Did this tip work for you?

Yes | No

Failed: 21st October, 2008

 

There must be no mention of when it last worked/failed until either "Yes" or "No" are actually clicked:

 

Did this tip work for you?

Yes | No

 

I realize that I need a table with 2 columns. For example:

 

create table input (
lastvalue char(1) not null,
lastdate date not null
);

 

And that I have to update the columns if "Yes" or "No" are clicked:

 

UPDATE input SET lastdate=curdate(), lastvalue=y/n;

 

However, I don't understand how to put together the rest of the code.

 

Can anyone help, please?

Link to comment
https://forums.phpfreaks.com/topic/126132-solved-do-you-know-how-to-do-this/
Share on other sites

How to put the rest of the code together depends on how you want to do this:

 

 

#1: Make a <form></form> and a "yes" and a "no" button. Then another script will be able to pick up the answer by looking at $_POST['yes_btn'] and $_POST['no_btn'].

 

#2: Make a link to something like this: yoursite.com/didtipwork.php?answer=yes&tid=1337 and yoursite.com/didtipwork.php?answer=no&tid=1337. This will allow you to look at $_GET['answer'] and take the appropriate action depending on the answer. This script could redirect the user back to the tip page by means of the tid (tip id)....

 

#3: Some nifty ajax solution which will use one of the above solutions but without the user noticing..

 

Which path do you want to take?

Thanks for the response, Wuhtzu!

 

Here's how I've tried it. The problem is, it doesn't work!  :'(

 

Try not to laugh, now!  :-[

 

Database:

 

create table input (
lastvalue char(1),
lastdate date
);

 

On tips.php:

 

<p>Worked for You?</p>

 

<p>

<a href="input.php?input=y">YES</a> |

<a href="input.php?input=n">NO</a>

</p>

 

<?php

$conn = mysql_connect('localhost','root') or trigger_error("SQL", E_USER_ERROR);
mysql_select_db('ctyi', $conn) or trigger_error("SQL", E_USER_ERROR);

$query = mysql_query("SELECT lastvalue, lastdate FROM input");
$data = mysql_fetch_assoc($query);

if ($data['lastvalue'] == ' ') {
    echo " ";

} elseif ($data['lastvalue'] == 'n') {
    echo "Failed: ";

} else {
    echo "Last worked: ";
}

echo $data['lastdate'];

mysql_close($conn);

?>

 

On input.php:

 

<?php

$input = intval($_GET['input']);

$conn = mysql_connect('localhost','root') or trigger_error("SQL", E_USER_ERROR);
mysql_select_db('ctyi', $conn) or trigger_error("SQL", E_USER_ERROR);

mysql_query("UPDATE input SET lastdate=curdate(), lastvalue='$input'");

$ref = $_SERVER['HTTP_REFERER'];
header( 'refresh: 0; url='.$ref);

mysql_close($conn);

?>

 

What's wrong with my code? More importantly, have I done it the optimal way? What would you change? In particular, would you use the same redirect method on input.php?

The question is what problems are you experiencing?

 

Don't you get any output, does your code throw errors?

 

We can't run your code meaningfully because we do not have your tables ect. and it would take some time to setup your environment.

 

Please elaborate your problem :)

On tips.php, it just says:

 

Worked for You?

 

YES | NO

Last worked:

 

No matter what I click, it says the same thing. The database doesn't get updated and the output doesn't change.

 

It shouldn't even say "Last worked:". That should appear, along with a date, only when some clicks "Yes" or "No".

 

What could be the problem? What's wrong with my script? Any help will be appreciated.

OK, guys. I've managed to find the solution for this one. I wasn't using $_GET properly. Instead of

 

$input = intval ($_GET['input']);

 

it should have been

 

$input = $_GET['input'];

 

Or, at least that's all I changed for it to work.

 

I still have a problem, though . . . The redirect is very obvious. Is there a way for me to do this seemlessly?

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.