Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/271277-php-variable-is-not-parsed/
Share on other sites

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.

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 by PFMaBiSmAd

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? :sweat:

Edited by DrRossi

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 by DrRossi

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.

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.

:o

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 :blink:) 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 by DrRossi

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.

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.