Jump to content

Declaring a value script to use that is included


ztimer

Recommended Posts

Hi,

 

Im in trouble with a script. Mainly the problem is that the declared value is not reachable.

 

lets sai i have main.php file where i declare that

 

$user_id = '22';

and then i include a file that needs to get that value to work

include('somescript.php');

 

now when i go over to the somescript.php i write at the top that print $user_id; and i get nothing.

 

What am i doing wrong?

 

 

Link to comment
Share on other sites

Assuming you are navigating to main.php and getting nothing, I would say that the file is not "including" correctly.

Can you show us the actual code where the issue is occurring?

 

For the sake of testing, try wrapping the include in an if statement

if (!include('somescript.php')) {
echo 'include failed.';
}

Link to comment
Share on other sites

ok i just realized that it is more complicated than that.

I have a main.php file that will post the selection info to the poll.php file

 

<?php
$poll_user_id = 22;
?>

								<script type="text/javascript" >
								$(function(){
									var loader=$('#loader');
									var pollcontainer=$('#pollcontainer');
									loader.fadeIn();
									//Load the poll form
									$.get('./modules/poll/poll.php', '', function(data, status){
										pollcontainer.html(data);
										animateResults(pollcontainer);
										pollcontainer.find('#viewresult').click(function(){
											//if user wants to see result
											loader.fadeIn();
											$.get('./modules/poll/poll.php', 'result=1', function(data,status){
												pollcontainer.fadeOut(1000, function(){
													$(this).html(data);
													animateResults(this);
												});
												loader.fadeOut();
											});
											//prevent default behavior
											return false;
										}).end()
										.find('#pollform').submit(function(){
											var selected_val=$(this).find('input[name=poll]:checked').val();
											if(selected_val!=''){
												//post data only if a value is selected
												loader.fadeIn();
												$.post('./modules/poll/poll.php', $(this).serialize(), function(data, status){
													$('#formcontainer').fadeOut(100, function(){
														$(this).html(data);
														animateResults(this);
														loader.fadeOut();
													});
												});
											}
											//prevent form default behavior
											return false;
										});
										loader.fadeOut();
									});

									function animateResults(data){
										$(data).find('.bar').hide().end().fadeIn('slow', function(){
															$(this).find('.bar').each(function(){
																var bar_width=$(this).css('width');
																$(this).css('width', '0').animate({ width: bar_width }, 1000);
															});
														});
									}

								});
								</script>

I think i must add the user id here to be posted with the selection ? and some sort of $get to the poll.php file.

 

POLL.PHP

<?php

$conn=mysql_connect('localhost', 'xxx', 'xxx') or die("Can't connect to mysql host");
mysql_select_db('polls') or die("Andmebaasiga ei suuda suhelda");

if(!$_POST['poll'] || !$_POST['pollid']){
$query=mysql_query("SELECT id, ques FROM questions ORDER BY id DESC LIMIT 1");
while($row=mysql_fetch_assoc($query)){
	echo "<p class=\"pollques\" >".$row['ques']."</p>";
	$poll_id=$row['id'];
}

if($_GET["result"]==1 || $_COOKIE["voted".$poll_id]=='yes'){
	//if already voted or asked for result
	showresults($poll_id);
	exit;
}

else{
//display options with radio buttons
	$query=mysql_query("SELECT id, value FROM options WHERE ques_id=$poll_id");
	if(mysql_num_rows($query)){
		echo '<div id="formcontainer" ><form method="post" id="pollform" action="'.$_SERVER['PHP_SELF'].'" >';
		echo '<input type="hidden" name="pollid" value="'.$poll_id.'" />';
		while($row=mysql_fetch_assoc($query)){
			echo '<p><input type="radio" name="poll" value="'.$row['id'].'" id="option-'.$row['id'].'" /> 
			<label for="option-'.$row['id'].'" >'.$row['value'].'</label></p>';
		}
		echo '<p><input type="submit"  value="Submit" /></p></form>';
		echo '<p><a href="'.$_SERVER['PHP_SELF'].'?result=1" id="viewresult">Vaata tulemusi</a></p></div>';
	}
}
}
else{
if($_COOKIE["voted".$_POST['pollid']]!='yes'){

	//Check if selected option value is there in database?
	$query=mysql_query("SELECT * FROM options WHERE id='".intval($_POST["poll"])."'");
	if(mysql_num_rows($query)){
		$query="INSERT INTO votes(ques_id, option_id, date, user_id) VALUES('".$_POST['pollid']."', '".$_POST["poll"]."', '".date('Y-m-d H:i:s')."', '".$user_id."')";
		if(mysql_query($query))
		{
			//Vote added to database
			setcookie("voted".$_POST['pollid'], 'yes', time()+86400*300);				
		}
		else
			echo "Paringu teostamisel tekkis probleeme: ".mysql_error();
	}
}
showresults(intval($_POST['pollid']));
}
function showresults($poll_id){
global $conn;
$query=mysql_query("SELECT COUNT(*) as totalvotes FROM votes WHERE option_id IN(SELECT id FROM options WHERE ques_id='$poll_id')");
while($row=mysql_fetch_assoc($query))
	$total=$row['totalvotes'];
$query=mysql_query("SELECT options.id, options.value, COUNT(*) as votes FROM votes, options WHERE votes.option_id=options.id AND votes.option_id IN(SELECT id FROM options WHERE ques_id='$poll_id') GROUP BY votes.option_id");
while($row=mysql_fetch_assoc($query)){
	$percent=round(($row['votes']*100)/$total);
	echo '<div class="option" ><p>'.$row['value'].' (<em>'.$percent.'%,  "'.$row['votes'].'"</em> )</p>';
	echo '<div class="bar ';
	if($_POST['poll']==$row['id']) echo ' yourvote';
	echo '" style="width: '.$percent.'%; " ></div></div>';
}
echo '<p>Vastatud: '.$total.'</p>';
}

Link to comment
Share on other sites

From reading the code properly, I think you are attempting to send the $poll_user_id into the $('#pollform').submit()

What I would do.. is send it across in your first $.get() call like this

$.get('./modules/poll/poll.php', 'user_id=<?php echo $poll_user_id; ?>', function(data, status){

Then in your form generation part of POLL.PHP create a hidden element containing the $_GET['user_id'] data.

Link to comment
Share on other sites

Thanks. Seems it will post the poll_user_id to the poll.php and it shows up nicely now when i add $poll_user_id to be inserted to the database with the other data

it is disgarded for some reason. Any ideas.

 

main.php

                                                                               <?php
									$poll_user_id = "22";

									?>

									<script type="text/javascript" >
										$(function(){
											var loader=$('#loader');
											var pollcontainer=$('#pollcontainer');
											loader.fadeIn();
											//Load the poll form
											$.get('./modules/poll/poll.php', 'user_id=<?php echo $poll_user_id; ?>', function(data, status){
												pollcontainer.html(data);
												animateResults(pollcontainer);
												pollcontainer.find('#viewresult').click(function(){
													//if user wants to see result
													loader.fadeIn();
													$.get('./modules/poll/poll.php', 'result=1', function(data,status){
														pollcontainer.fadeOut(1000, function(){
															$(this).html(data);
															animateResults(this);
														});
														loader.fadeOut();
													});
													//prevent default behavior
													return false;
												}).end()
												.find('#pollform').submit(function(){
													var selected_val=$(this).find('input[name=poll]:checked').val();
													if(selected_val!=''){
														//post data only if a value is selected
														loader.fadeIn();
														$.post('./modules/poll/poll.php', $(this).serialize(), function(data, status){
															$('#formcontainer').fadeOut(100, function(){
																$(this).html(data);
																animateResults(this);
																loader.fadeOut();
															});
														});
													}
													//prevent form default behavior
													return false;
												});
												loader.fadeOut();
											});

											function animateResults(data){
												$(data).find('.bar').hide().end().fadeIn('slow', function(){
																	$(this).find('.bar').each(function(){
																		var bar_width=$(this).css('width');
																		$(this).css('width', '0').animate({ width: bar_width }, 1000);
																	});
												});
											}

										});
									</script>

									<div id="container" >
										<div class="col02 clear3">
										<h2>Küsitlusmoodul</td></h2><br>
										<div id="pollcontainer" >
										</div>
										<p id="loader" >Laen andmeid...</p>
									</div>

 

poll.php

<?php

	print $poll_user_id = $_GET['user_id']; // Works grear so far.

$conn=mysql_connect('localhost', 'xxx', 'xxxx') or die("Can't connect to mysql host");
mysql_select_db('polls') or die("Andmebaasiga ei suuda suhelda");

if(!$_POST['poll'] || !$_POST['pollid']){
$query=mysql_query("SELECT id, ques FROM questions ORDER BY id DESC LIMIT 1");
while($row=mysql_fetch_assoc($query)){
	echo "<h4><p class=\"pollques\" >".$row['ques']."</h4></p>";
	$poll_id=$row['id'];
}

if($_GET["result"]==1 || $_COOKIE["voted".$poll_id]=='yes'){
	//if already voted or asked for result
	showresults($poll_id);
	exit;
}

else{
//display options with radio buttons
	$query=mysql_query("SELECT id, value FROM options WHERE ques_id=$poll_id");
	if(mysql_num_rows($query)){
		echo '<div id="formcontainer" ><form method="post" id="pollform" action="'.$_SERVER['PHP_SELF'].'" >';
		echo '<input type="hidden" name="pollid" value="'.$poll_id.'" />';
		while($row=mysql_fetch_assoc($query)){
			echo '<p><input type="radio" name="poll" value="'.$row['id'].'" id="option-'.$row['id'].'" /> 
			<label for="option-'.$row['id'].'" >'.$row['value'].'</label></p>';
		}
		echo '<p><div align="right"><input type="submit"  value="Vasta kysimusele" class="button"/></div></p></form>';
		echo '<p><a href="'.$_SERVER['PHP_SELF'].'?result=1" id="viewresult">Vaata tulemusi</a></p></div>';
	}
}
}

else{
if($_COOKIE["voted".$_POST['pollid']]!='yes'){

	//Check if selected option value is there in database?
	$query=mysql_query("SELECT * FROM options WHERE id='".intval($_POST["poll"])."'");
	if(mysql_num_rows($query)){


                 // -> Not inserting the $poll_user_id for some strange reason ??

		$query="INSERT INTO votes(ques_id, option_id, date, user_id) VALUES('".$_POST['pollid']."', '".$_POST["poll"]."', '".date('Y-m-d H:i:s')."', '".$poll_user_id."')";
		if(mysql_query($query))
		{
			//Vote added to database
			setcookie("voted".$_POST['pollid'], 'yes', time()+86400*300);				
		}
		else
			echo "Paringu teostamisel tekkis probleeme: ".mysql_error();
	}
}
showresults(intval($_POST['pollid']));
}
function showresults($poll_id){
global $conn;
$query=mysql_query("SELECT COUNT(*) as totalvotes FROM votes WHERE option_id IN(SELECT id FROM options WHERE ques_id='$poll_id')");
while($row=mysql_fetch_assoc($query))
	$total=$row['totalvotes'];
$query=mysql_query("SELECT options.id, options.value, COUNT(*) as votes FROM votes, options WHERE votes.option_id=options.id AND votes.option_id IN(SELECT id FROM options WHERE ques_id='$poll_id') GROUP BY votes.option_id");
while($row=mysql_fetch_assoc($query)){
	$percent=round(($row['votes']*100)/$total);
	echo '<div class="option" ><p>'.$row['value'].' (<em>'.$percent.'%,  "'.$row['votes'].'"</em> )</p>';
	echo '<div class="bar ';
	if($_POST['poll']==$row['id']) echo ' yourvote';
	echo '" style="width: '.$percent.'%; " ></div></div>';
}
echo '<p>Vastatud: '.$total.'</p>';
}

 

Problem is now that the $poll_user_id shows up at the page but it will not insert it to the database. It should work for all i know.

All the other data is inserted pollid, poll and date.

Link to comment
Share on other sites

You are using GET in the first instance, which is fine but when you use $('#pollform').submit() you are posting the data, in which case you should access the POST value from the hidden element that was created from the initial jQuery GET

 

Hi, Seems i needed some time to figure out what you really told me in the last post. What i did was in poll.php I added a hidden textbox containing the retreived user_id so it will

post it and then i used the $_POST['poll_user_id'] to enter the user id to the database.

 

<?php

$poll_user_id = $_GET['user_id'];

$conn=mysql_connect('localhost', 'xxx', 'xxx') or die("Can't connect to mysql host");
mysql_select_db('polls') or die("Andmebaasiga ei suuda suhelda");

if(!$_POST['poll'] || !$_POST['pollid']){

	$query=mysql_query("SELECT id, ques FROM questions ORDER BY id DESC LIMIT 1");
	while($row=mysql_fetch_assoc($query)){
		echo "<h4><p class=\"pollques\" >".$row['ques']."</h4></p>";
		$poll_id=$row['id'];
	}

	if($_GET["result"]==1 || $_COOKIE["voted".$poll_id]=='yes'){

		//if already voted or asked for result
		showresults($poll_id);
		exit;

	}else{ 

		//display options with radio buttons
		$query=mysql_query("SELECT id, value FROM options WHERE ques_id=$poll_id");

		if(mysql_num_rows($query)){
			echo '<div id="formcontainer" ><form method="post" id="pollform" action="'.$_SERVER['PHP_SELF'].'" >';
			echo '<input type="hidden" name="pollid" value="'.$poll_id.'" />';
			echo '<input type="hidden" name="poll_user_id" value="'.$poll_user_id.'" />';
			while($row=mysql_fetch_assoc($query)){
				echo '<input type="radio" name="poll" value="'.$row['id'].'" id="option-'.$row['id'].'" /> 
				<label for="option-'.$row['id'].'" >'.$row['value'].'</label><br>';
			}
			echo '<p><div align="right"><input type="submit"  value="Vasta kysimusele" class="button"/></div></p></form>';
			echo '<p><a href="'.$_SERVER['PHP_SELF'].'?result=1" id="viewresult">Vaata tulemusi</a></p></div>';
		}
	}

}else{

	if($_COOKIE["voted".$_POST['pollid']]!='yes'){

		//Check if selected option value is there in database?
		$query=mysql_query("SELECT * FROM options WHERE id='".intval($_POST["poll"])."'");
		if(mysql_num_rows($query)){
			$query="INSERT INTO votes(ques_id, option_id, date, user_id) VALUES('".$_POST['pollid']."', '".$_POST["poll"]."', '".date('Y-m-d H:i:s')."', '".$_POST['poll_user_id']."')";
			if(mysql_query($query))
			{
				//Vote added to database
				setcookie("voted".$_POST['pollid'], 'yes', time()+86400*300);				
			}
			else
				echo "Paringu teostamisel tekkis probleeme: ".mysql_error();
		}
	}
	showresults(intval($_POST['pollid']));
}

function showresults($poll_id){
	global $conn;
	$query=mysql_query("SELECT COUNT(*) as totalvotes FROM votes WHERE option_id IN(SELECT id FROM options WHERE ques_id='$poll_id')");
	while($row=mysql_fetch_assoc($query))
		$total=$row['totalvotes'];
	$query=mysql_query("SELECT options.id, options.value, COUNT(*) as votes FROM votes, options WHERE votes.option_id=options.id AND votes.option_id IN(SELECT id FROM options WHERE ques_id='$poll_id') GROUP BY votes.option_id");
	while($row=mysql_fetch_assoc($query)){
		$percent=round(($row['votes']*100)/$total);
		echo '<div class="option" ><p>'.$row['value'].' (<em>'.$percent.'%,  "'.$row['votes'].'"</em> )</p>';
		echo '<div class="bar ';
		if($_POST['poll']==$row['id']) echo ' yourvote';
		echo '" style="width: '.$percent.'%; " ></div></div>';
	}
	echo '<p>Vastatud: '.$total.'</p>';
}

 

Thank you so much for your help. Now i can move on with the code and i'm a bit smarter from the process. :)

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.