ztimer Posted September 27, 2011 Share Posted September 27, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/247934-declaring-a-value-script-to-use-that-is-included/ Share on other sites More sharing options...
Buddski Posted September 27, 2011 Share Posted September 27, 2011 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.'; } Quote Link to comment https://forums.phpfreaks.com/topic/247934-declaring-a-value-script-to-use-that-is-included/#findComment-1273134 Share on other sites More sharing options...
ztimer Posted September 27, 2011 Author Share Posted September 27, 2011 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/247934-declaring-a-value-script-to-use-that-is-included/#findComment-1273140 Share on other sites More sharing options...
Buddski Posted September 27, 2011 Share Posted September 27, 2011 Yes, you will have to post the user_id with the form if you want to get it on the other end. Quote Link to comment https://forums.phpfreaks.com/topic/247934-declaring-a-value-script-to-use-that-is-included/#findComment-1273141 Share on other sites More sharing options...
ztimer Posted September 27, 2011 Author Share Posted September 27, 2011 Do you have any idea how to do it . Usual post wont work here.? Quote Link to comment https://forums.phpfreaks.com/topic/247934-declaring-a-value-script-to-use-that-is-included/#findComment-1273148 Share on other sites More sharing options...
Buddski Posted September 27, 2011 Share Posted September 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247934-declaring-a-value-script-to-use-that-is-included/#findComment-1273153 Share on other sites More sharing options...
ztimer Posted September 27, 2011 Author Share Posted September 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247934-declaring-a-value-script-to-use-that-is-included/#findComment-1273165 Share on other sites More sharing options...
Buddski Posted September 27, 2011 Share Posted September 27, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/247934-declaring-a-value-script-to-use-that-is-included/#findComment-1273196 Share on other sites More sharing options...
ztimer Posted September 27, 2011 Author Share Posted September 27, 2011 Sorry i don't understand. Seems that i'm just dumb if it comes to this sort of posting and retrieving. Quote Link to comment https://forums.phpfreaks.com/topic/247934-declaring-a-value-script-to-use-that-is-included/#findComment-1273308 Share on other sites More sharing options...
ztimer Posted September 28, 2011 Author Share Posted September 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247934-declaring-a-value-script-to-use-that-is-included/#findComment-1273446 Share on other sites More sharing options...
Buddski Posted September 28, 2011 Share Posted September 28, 2011 Glad it made sense.. I had a few beers last night so it might have sounded a bit like drivel. Quote Link to comment https://forums.phpfreaks.com/topic/247934-declaring-a-value-script-to-use-that-is-included/#findComment-1273447 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.