Avatar of dolan2go
dolan2go
Flag for United States of America asked on

Javascript - Fill text value from radio button select

This is a combination of 2 questions previously asked.

I want the selection of one radio button to add a mysql query to the text box below. I've gotten to the point that one query (#2), while filling the input > text > value of the php/html source, does not appear in the text box.

The further issue is how the query gets processed by php and then mysql. At this point , the 2nd query reports 1065, empty query, after inserting and submitting the code below thru the php script. The same code, returns 19 rows in phpMyAdmin.

.
SELECT a.email1, a.empNum, b.runTime FROM User a, NonRev_Check_in b WHERE b.runTime LIKE '%11-14%' AND a.empNum = b.empNum

Open in new window


Here's a shortened version of the php script.

$query_1 = "SELECT email1 FROM User WHERE empNum = 354";
$query_2 = "SELECT a.email1, a.empNum, b.runTime FROM User a, NonRev_Check_in b WHERE b.runTime LIKE '\%11-14\%' AND a.empNum = b.empNum";
$query_3 = "SELECT email1, listingPhone FROM User WHERE empNum = 354";

if ( $_POST['submit']
{
   $connect = mysql_connect ( $host, $db_user, $db_password );
   mysql_select_db ( $database );

   if ( isset ( $_POST['query'] ) )
   {
      $query = mysql_real_escape_string ( $_POST['query'] );
   }
   echo $query . "<br />";
}

<form name="query" action="/admin/email.php" method="post">
   <input type="radio" name="que" onclick="if(this.checked)document.getElementById('query').value='<?php print $query_1 ?>';" /> Email &nbsp;
   <input type="radio" name="que" onclick="if(this.checked)document.getElementById('query').value='<?php print $query_2 ?>';" /> Check-in &nbsp;
   <input type="radio" name="que" onclick="if(this.checked)document.getElementById('query').value='<?php print $query_3 ?>';" /> Phone &nbsp;
   <br />
   Query: &nbsp; 
   <input type="text" id="query" size="50" name="query" value="" />
   <input type="submit" name="submit" value="Submit" />
</form>

Open in new window


Thanks for looking.

JavaScriptMySQL ServerPHP

Avatar of undefined
Last Comment
dolan2go

8/22/2022 - Mon
Mark Brady

From looking at your code it looks correct. Try moving the semi-colon from the 3 lines starting with

   <input type="radio" name="que" onclick="if(this.checked)document.getElementById('query').value='<?php print $query_1 ?>';" /> Email &nbsp

move the ; so it should look like this:

   <input type="radio" name="que" onclick="if(this.checked)document.getElementById('query').value='<?php print $query_1; ?>'/>" Email &nbsp

I moved the semi colon to the end of the print statement so it should run properly. I also change the location of your closing /> at the end of the input field. Anyway, do that to all 3 line of code and try it again.

<?php
print $query_1;
?>
Prograministrator

Hello,

Find no way to escaping illegal character problem that found in your 2ed query,

except this way :

<?php
$query_1 = "SELECT email1 FROM User WHERE empNum = 354";
$query_2 = "SELECT a.email1, a.empNum, b.runTime FROM User a, NonRev_Check_in b WHERE a.empNum = b.empNum AND b.runTime LIKE";
$query_3 = "SELECT email1, listingPhone FROM User WHERE empNum = 354";

if ( $_POST['submit'])
{
   $connect = mysql_connect ( $host, $db_user, $db_password );
   mysql_select_db ( $database );

   if ( isset ( $_POST['query'] ) )
   {
      $query = mysql_real_escape_string ( $_POST['query'] );
   }
   echo $query . "<br />";
}
?>
<script>
var test = "'% 11-14 %';";
</script>
<form name="query" action="" method="post">
    <input type="radio" name="que" onclick="if(this.checked)document.getElementById('query').value='<?php print $query_1; ?>'" > Email &nbsp;
    <input type="radio" name="que" onclick="if(this.checked)document.getElementById('query').value='<?php print $query_2; ?>'+test" > Check-in &nbsp;
    <input type="radio" name="que" onclick="if(this.checked)document.getElementById('query').value='<?php print $query_3; ?>'" > Phone &nbsp;
   <br />
   Query: &nbsp; 
   <input type="text" id="query" size="50" name="query" value="" />
   <input type="submit" name="submit" value="Submit" />
</form>

Open in new window


Of course, it's not a professional way but since you are viewing your queries for users, so I assume that it's a good way for you and doing what you want.
dolan2go

ASKER
@elvin66,

Your suggestion didn't change the non-visible #2 query. I didn't follow it with the multiple single and double quotes. The suggestion seems to put them out of order, at the end of the javascript in the input tag.

@Prograministrator,

The end of the query (for #2) is now: .....LIKE [object HTMLInputElement]

Thanks to both for suggestions.

The issue is the % in the query.

I am still looking for an solution and then on to whether the mysql query works.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Prograministrator

What's your browser?

because, I tested it on Firefox and chrome and it worked fine.

Place your final tested code please.
ASKER CERTIFIED SOLUTION
dsmile

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
dsmile

This is working code
<?php
$query_1 = "SELECT email1 FROM User WHERE empNum = 354";
$query_2 = "SELECT a.email1, a.empNum, b.runTime FROM User a, NonRev_Check_in b WHERE b.runTime LIKE \'\%11-14\%\' AND a.empNum = b.empNum";
$query_3 = "SELECT email1, listingPhone FROM User WHERE empNum = 354";


if ( $_POST['submit'])
{
	$host = 'localhost'; $db_user = 'root'; $db_password = '';
	$database = 'test';
   $connect = mysql_connect ( $host, $db_user, $db_password );
   mysql_select_db ( $database );

   if ( isset ( $_POST['query'] ) )
   {
      $query = mysql_real_escape_string ( str_replace("\'", "'", $_POST['query'] ));
   }
   echo $query . "<br />";
}
?>
<form name="query" action="?" method="post">
   <input type="radio" name="que" onclick="if(this.checked)document.getElementById('query').value='<?php print $query_1 ?>';" /> Email &nbsp;
   <input type="radio" name="que" onclick="if(this.checked)document.getElementById('query').value='<?php print $query_2 ?>';" /> Check-in &nbsp;
   <input type="radio" name="que" onclick="if(this.checked)document.getElementById('query').value='<?php print $query_3 ?>';" /> Phone &nbsp;
   <br />
   Query: &nbsp; 
   <input type="text" id="query" size="50" name="query" value="" />
   <input type="submit" name="submit" value="Submit" />
</form>

Open in new window

dolan2go

ASKER
dsmile,

Your suggestion is right on.

Thank you.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.