Solved

Javascript - Fill text value from radio button select

Posted on 2010-11-14
7
647 Views
Last Modified: 2012-05-10
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.

0
Comment
Question by:dolan2go
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 20

Expert Comment

by:Mark Brady
ID: 34132033
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;
?>
0
 
LVL 3

Expert Comment

by:Prograministrator
ID: 34132797
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.
0
 
LVL 1

Author Comment

by:dolan2go
ID: 34133099
@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.
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 3

Expert Comment

by:Prograministrator
ID: 34133198
What's your browser?

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

Place your final tested code please.
0
 
LVL 13

Accepted Solution

by:
dsmile earned 500 total points
ID: 34135103
Your second query should be like this

$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";

Single quotes in the query must be escaped or it will break generated html code
0
 
LVL 13

Expert Comment

by:dsmile
ID: 34135143
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

0
 
LVL 1

Author Closing Comment

by:dolan2go
ID: 34136624
dsmile,

Your suggestion is right on.

Thank you.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now