DrRossi Posted November 28, 2012 Share Posted November 28, 2012 Hello, I am trying to pass PHP variables in a jquery function with the javascript onclick but its not working(i know it sounds confusing but I will explain). I define my $output in first.php then echo it in second.php. Here is my first.php $answer = "some text"; $output .= '<div id="'.$k.'"> <form id='.$f.' > <input type="radio" name="accuracy" value="yes" checked>Accurate <input type="radio" name="accuracy" value="no">Not Accurate <input type="button" id='.$j.' value="Submit" onclick="sendData('.$answer.')"> </form> </div>'; My jquery function in second.php function sendData(feel) { $.get("test.php", { feeling: 'feel'} ); } My test.php : $f= $_GET["feeling"]; $con = mysql_connect('localhost', 'username', 'pass'); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("mydatabase", $con); $sql="INSERT INTO testtable (testcolumn) VALUES ('$f')"; $result = mysql_query($sql); echo '<p2> Thank you. Your opinion is much appreciated!</p2>'; mysql_close($con); An entry is added in the database but with an empty value. Any idea where is the problem? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/271277-php-variable-is-not-parsed/ Share on other sites More sharing options...
MDCode Posted November 28, 2012 Share Posted November 28, 2012 (edited) Single quoted variables do not get parsed. Do you have error reporting on? Edited November 28, 2012 by SocialCloud Quote Link to comment https://forums.phpfreaks.com/topic/271277-php-variable-is-not-parsed/#findComment-1395769 Share on other sites More sharing options...
DrRossi Posted November 28, 2012 Author Share Posted November 28, 2012 then why it works for the other variables like the div,form and button id? Quote Link to comment https://forums.phpfreaks.com/topic/271277-php-variable-is-not-parsed/#findComment-1395771 Share on other sites More sharing options...
PFMaBiSmAd Posted November 28, 2012 Share Posted November 28, 2012 The $answer variable inside of the sendData(...) function call needs to be enclosed by literal single-quotes in the HTML, so that is treated as a string. Now, the value echoed out in $answer is bing treated as javascript variables/identifiers. Do a 'view source' of the output in your browser to make sure it is what you expect. Quote Link to comment https://forums.phpfreaks.com/topic/271277-php-variable-is-not-parsed/#findComment-1395772 Share on other sites More sharing options...
DrRossi Posted November 28, 2012 Author Share Posted November 28, 2012 Can you show me what do you mean please? Because the $answer is already enclosed in single quotes and is treated as a variable. Also the $answer comes up correctly in the source code so the problem must be either with the jquery or with my sql. Quote Link to comment https://forums.phpfreaks.com/topic/271277-php-variable-is-not-parsed/#findComment-1395781 Share on other sites More sharing options...
PFMaBiSmAd Posted November 28, 2012 Share Posted November 28, 2012 (edited) This is what the 'view source' is now - <input type="button" id= value="Submit" onclick="sendData(some text)"> This is what it needs to be - <input type="button" id= value="Submit" onclick="sendData('some text')"> Edited November 28, 2012 by PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/271277-php-variable-is-not-parsed/#findComment-1395784 Share on other sites More sharing options...
DrRossi Posted November 28, 2012 Author Share Posted November 28, 2012 (edited) I see.... This is what i get when i do a view source: <input type="button" id="104" value="Submit" onclick="sendData(1)"> but you say that it needs to be <input type="button" id="104" value="Submit" onclick="sendData('1')"> right? how I am going to do that? Edited November 28, 2012 by DrRossi Quote Link to comment https://forums.phpfreaks.com/topic/271277-php-variable-is-not-parsed/#findComment-1395785 Share on other sites More sharing options...
PFMaBiSmAd Posted November 28, 2012 Share Posted November 28, 2012 If the value is actually a number, that part of it will work without the quoting. To add the quoting - <input type="button" id='.$j.' value="Submit" onclick="sendData(\''.$answer.'\')"> Quote Link to comment https://forums.phpfreaks.com/topic/271277-php-variable-is-not-parsed/#findComment-1395788 Share on other sites More sharing options...
DrRossi Posted November 28, 2012 Author Share Posted November 28, 2012 (edited) it is a number but I am going to add some text as well. Well the thing is that as I said earlier, in my second.php which I echo my $output, if I view the source the $answer is correctly there like <input type="button" id="104" value="Submit" onclick="sendData(1)"> but its not adding it in the database. It adds an empty value instead. The problem seems to be with the jquery function function sendData(feel) { $.get("test.php", { feeling: 'feel'} ); } or with my sql code any ideas? I appreciate your help. Thanks Edited November 28, 2012 by DrRossi Quote Link to comment https://forums.phpfreaks.com/topic/271277-php-variable-is-not-parsed/#findComment-1395790 Share on other sites More sharing options...
PFMaBiSmAd Posted November 28, 2012 Share Posted November 28, 2012 The next problem will be the single quotes around 'feel' in the following - $.get("test.php", { feeling: 'feel'} ); That's a string consisting of the letters f, e, e, and l. You would need to remove those single-quotes so that the variable feel would be used at that point. Quote Link to comment https://forums.phpfreaks.com/topic/271277-php-variable-is-not-parsed/#findComment-1395792 Share on other sites More sharing options...
DrRossi Posted November 28, 2012 Author Share Posted November 28, 2012 Oh yea, it worked but I could swear that I tried that before I will try now to do the same with text in the string to see if it works. You are awsome PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/271277-php-variable-is-not-parsed/#findComment-1395793 Share on other sites More sharing options...
DrRossi Posted November 28, 2012 Author Share Posted November 28, 2012 Ok, text string works perfectly with the \' as you suggested. The only strange thing that is happening is that along with the normal entry in the database, another empty entry is added for each one. Quote Link to comment https://forums.phpfreaks.com/topic/271277-php-variable-is-not-parsed/#findComment-1395798 Share on other sites More sharing options...
DrRossi Posted November 28, 2012 Author Share Posted November 28, 2012 (edited) Any thoughts why I have 2 entries in my database (1 correct and 1 empty) for each of my buttons clicked? Edited November 28, 2012 by DrRossi Quote Link to comment https://forums.phpfreaks.com/topic/271277-php-variable-is-not-parsed/#findComment-1396025 Share on other sites More sharing options...
PFMaBiSmAd Posted November 29, 2012 Share Posted November 29, 2012 Your code is requesting the test.php page a second time. Since the <form tag in that code is incomplete, that would imply that all the code you have posted is in test.php? If so, when you first browse to the page, your database logic is being unconditionally executed and is inserting a row with empty data. You need to test and validate the input data so that your database logic only runs when you expect it to. Quote Link to comment https://forums.phpfreaks.com/topic/271277-php-variable-is-not-parsed/#findComment-1396063 Share on other sites More sharing options...
DrRossi Posted November 29, 2012 Author Share Posted November 29, 2012 (edited) You are a GURU. I mean you can solve problems without even seeing the code?! Solved that too. I was requesting the test.php page a second time as you said but I have added an if statement in my test.php in order to execute the INSERT query only when the "real stuff" were defined. If you could give me your opinion on one last problem I have I would be even more grateful to you. My last problem (hopefully ) is that I want to get the value of the radio button that was checked in the javascript function in order to pass it to test.php as well. Is there a simple way or I need an extra function in my function or something? Thanks Edited November 29, 2012 by DrRossi Quote Link to comment https://forums.phpfreaks.com/topic/271277-php-variable-is-not-parsed/#findComment-1396069 Share on other sites More sharing options...
DrRossi Posted November 29, 2012 Author Share Posted November 29, 2012 I tried at least 4 different ways, 1 of them which didn't work is: var answer = $("input[@name=accuracy]:checked").val(); It didnt submit anything. If i manually define var answer to "YES" or something it works. So the problem must be on how to check if my radio button its checked and get its value. Quote Link to comment https://forums.phpfreaks.com/topic/271277-php-variable-is-not-parsed/#findComment-1396100 Share on other sites More sharing options...
DrRossi Posted December 2, 2012 Author Share Posted December 2, 2012 removed the "@" sign and worked perfect. Thank you all!! Quote Link to comment https://forums.phpfreaks.com/topic/271277-php-variable-is-not-parsed/#findComment-1396969 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.