I have this javascript that i want to run in php,
the code bellow is supposed to be submitted in a form and then then prints it
but when submitted the javascript doesn't execute, the output is simply
var text = document.getElementById(\'course1\').options[document.getElementById(\'course1\').selectedIndex].text; document.write(text);
this is the whole thing,
echo "<form name\"find\" action=\"postEnrolled.php\" method=\"get\" class=\"required\" onsubmit=\"return validate(this);\">";
echo "<table width=\"225\" border=\"0\" align=\"center\" >";
echo "<tr>";
echo "<td width=\"181\"><label>Course#1:</label> <select name=\"dep1\" style=\"width:190px\" class=\"dep1\">";
echo "<option selected=\"selected\" value=\"\">Select Department</option>";
include('db.php');
$sql=mysql_query("select id,data from data where weight='1'");
while($row=mysql_fetch_array($sql))
{
$id = $row['id'];
$data = $row['data'];
echo '<option value="'.$id.'">'.$data.'</option>';
}
echo "</select><br/></td>";
echo "<td width=\"267\">";
echo "<label> </label><select name=\"course1\" class=\"course1\" style=\"width:200px\">";
echo "<option selected=\"selected\" value=\"\">Select Course</option>";
echo "</select>";
echo "<input type=\"hidden\" name=\"course_1\" value=\"
<script language='javascript' >
var text = document.getElementById('course1').options[document.getElementById('course1').selectedIndex].text;
document.write(text);
</script>
Am I missing something?
what I really want is to submit the text in the options and not the value of the options.
getElementById will only find an element whose id is course_1, not the name.
Don't put the script element inside the input element
You must have the DOM ready when calling it (use document.onload=function(){...yourcodehere...};)
At first sight, there is no PHP really involved in this problem. But are you aware that the code, as it is, wouldn't be executed when you change the value of the option ? If that's what you need, use onchange="yourcodehere;". But as it is an hidden field, maybe you should describe what you really want to achieve.
EDIT :
If what you want is change the hidden input when the user selects another option, here's how you can do it :
<input type=hidden name=course_1 id=course_1>
<select onchange="document.getElementById('course_1').value=this.options[this.selectedIndex].text;" ...
Your problem is that you're putting a <script> tag inside of the value attribute of the <input> tag. That isn't valid HTML or JavaScript and will not work.
why you don't post what is the actual error you got? actually this approach (including the javascript code into php tags) is not good at all.if you want to use javascript on any page, you just have to put it on the very top of your page under the script tags.
try it, will help u ..!
Related
I need to get values from a postgre db and use the fopen to open the link inside its records to open real xml file.
<?php
echo "<form id=read2 method=post action=read2.php>";
//other html and table codes
while ($row = pg_fetch_row($result)) {
echo "<tr><td>$row[1]</td><td><input type=hidden name=data value=$row[3] /><a href=javascript:; onclick=document.getElementById('read2').submit();>$row[2]</a></td><td>$row[4]</td><td>$row[5]</td></tr>";
}
the read2.php:
<?php
$data=$_POST['data'];
$explode = explode("/inbox/", $data);
$final = "";
$final.="data/";
$final.=$explode[1];
echo "Result of data before explode is: $data <br />";
echo "Result of data after explode is: $explode[1] <br /><br />";
$myfile = fopen("$final", "r") or die("<h1>Unable to open file!</h1>");
$xml = htmlspecialchars(fread($myfile,filesize("$final")));
?>
<pre>
<?php echo $xml; ?>
</pre>
<?php fclose($myfile); ?>
My problem is here: <input type=hidden name=data value=$row[3] />
I am able to pass to read2.php the correct value and use the explode to adjust what I really need, but I am not able to chose which value to get due to name=data which is the same for all and the read2.php only get the last one in list.
I tried with a counter inside the while: name=data[count]; count++
But in this case I do not know how to get "name" from $_POST
It is also possible that the javascript code I use to send the form is not the best for this situation. Can you please help me fix?
I think I understand what the problem is but please excuse me if I'm off target with what follows. If you were to approach this slightly differently it should be relatively easy.
Have one form separate from the table that contains the recordset data and links - the form need only have the one hidden element which you target using javscript and set the value dynamically.
The following is not tested so it might not work "out of the box" but I think the idea is sound.
<!doctype html>
<html>
<head>
<title>Table data - form submission</title>
<script type='text/javascript'>
function submitdata(event){
var el=typeof( event.target )!='undefined' ? event.target : event.srcElement;
var data=document.getElementById('data');
data.value=el.dataset.value;
data.parentNode.submit();
}
</script>
</head>
<body>
<?php
echo "
<!--
this is the form that actually sends data.
Programatically set the value of the hidden element
and submit the form
-->
<form name='senddata' method='post' action='read2.php'>
<input type='hidden' id='data' name='data'/>
</form>
<table>";
while( $row = pg_fetch_row( $result ) ) {
echo "
<tr>
<td>{$row[1]}</td>
<td>
<a href='#' data-value='{$row[3]}' onclick='submitdata(event)'>{$row[2]}</a>
</td>
<td>{$row[4]}</td>
<td>{$row[5]}</td>
</tr>";
}
echo "
</table>";
?>
</body>
</html>
You should move the hidden parameter from multiple to single and set the hidden parameter value using jquery or javascript and submit the form. Try as below :
<?php
echo "<form id='read2' method='post' action='read2.php'>";
echo "<input type='hidden' name='data' id='data'/>";
echo '<table>';
while ($row = pg_fetch_row($result)) {
echo "<tr><td>$row[1]</td><td><a dataval=$row[2] href='#' onclick='callfunc(this)'>$row[2]</a></td><td>$row[4]</td><td>$row[5]</td></tr>";
}
echo '</table>';
echo '</form>';
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
function callfunc(obj)
{
var dataval = $(obj).attr('dataval');
$('#data').val(dataval);
$('#read2').submit();
}
</script>
Above code is tested and working.
I have the following program, it searchs for the text placed in a previous php file, and it displays the results, by adding a radiobox to check the item that will be purchased. I am not able to make the page save the item that was checked from the items found into a new table, I don't know how to do that, because the items found are placed as fetched items, therefore I don't know how to select one to save the entire row selected. Please help!.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Search option</title>
</head>
<body>
<?php
echo "<form action='slips.php' method='post'>";
if(isset($_POST['name_prod2'])){
$word=$_POST['name_prod2'];
$conn = oci_pconnect('dbname', 'password', 'localhost/XE');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['Error'], ENT_QUOTES), E_USER_ERROR);
}
$stid = oci_parse($conn, "SELECT * FROM product WHERE LOWER(name) LIKE '%" . $word . "%'");
oci_execute($stid);
echo "<table width='950' table border='1' align='center'>\n";
echo "<tr>\n";
echo "<th width='50'> <div align='center'>buy</div></th>";
echo "<th width='110'> <div align='center'>Product ID</div></th>";
echo "<th width='190'> <div align='center'>Product name</div></th>";
echo "<th width='250'> <div align='center'>Description</div></th>";
echo "<th width='100'> <div align='center'>in Store</div></th>";
echo "<th width='100'> <div align='center'>price</div></th>";
echo "<th width='190'> <div align='center'>Quantity to purchase</div></th>";
echo "</tr>\n";
while ($product = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
echo "<tr>\n";
//echo "<td><div style='text-align:center'><label><input type='radio' name='radio1' value='valor'></label></td></div>";
echo sprintf('<td><div style="text-align:center"><label><input type="radio" name="product" value="%s"></label></td></div>', $product['product_id']);
foreach ($product as $aspect) {
echo '<td><div style="text-align:center">'.($aspect !== null ? htmlentities($aspect, ENT_QUOTES) : '')."</td></div>\n";
}
echo '<td width="50"><div align="center"><input name="quantity" type="text" size="27" maxlength="50" placeholder="Enter quantity"></div></td>';
}
echo "</table>\n";
}
echo "<div style='text-align:center'><input type='submit' value='Comprar'></div>";
echo"</form>";
?>
</body>
</html>
These are two of the Javascripts that I have tried so far to complete this, but they fail to tell me when one has been selected, I don't know if I can try adding this code to a button, and when I click it, it will tell me which row from the radio box was checked, and then save it:
<script type="text/javascript" src="js/jquery.js"> </script>
<script type="text/javascript">
var user_cat = $("input[radio1='user_cat']:checked").val();
if (!$("input[radio1='radio1']").is(':checked')) {
alert('Nothing is checked!');
}
else {
alert('One of the radio buttons is checked!');
}
$(document).ready(function() {
$('#btnStatus').click(function(){
var isChecked = $('#rdSelect').prop('checked');
alert(isChecked);
});
});
</script>
When is use this line echo "<td><div style='text-align:center'><label><input type='radio' name='radio1' value='valor'></label></td></div>"; it works and displays this results
But when I use this line echo sprintf('<td><div style="text-align:center"><label><input type="radio" name="product" value="%d"></label></td></div>', $product['product_id']);it doesn't work and displays this results
OK, picking up the information from the comments to the question and doing a little guess work I will try to point you into the right direction. It is not possible to give a read-to-use answer, since still there are things not clear, but let's have a try to get started...
I see you have an html form which includes a table. That table has a header row and dynamic generated rows holding some product information each. You want to have a radio button in front of each row to allow to select a row. And you want a text input field at the end of each row which allows to enter a quantity. Then you want to post that information to the server to be able to process it.
I will stick with the "conservative" html approach and not introduce scripting here. Reason is that for the purpose described before that is not required. So let's keep things simple. Obviously nothing speaks against making things more complicated later on :-)
Your radio buttons have to be changed, they currently make no sense. You have to give the an individual value, so that you can identify which row has been selected later on. Currently you give them all the same static value 'value'. So change the loop that iterates over the products to something like:
while ($product = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
echo "<tr>\n";
echo sprintf('<td><div style="text-align:center"><label><input type="radio" name="product" value="%s"></label></td></div>'."\n", $product['product_id']);
foreach ($row as $aspect) {
echo '<td><div style="text-align:center">'
.($aspect !== null ? htmlentities($aspect, ENT_QUOTES) : '')
."</td></div>\n";
}
echo '<td width="50"><div align="center"><input name="quantity" type="text" size="27" maxlength="50" placeholder="Enter quantity"></div></td>'."\n";
}
Note: I took the liberty to change the chosen names to be more logical to product, aspect and quantity...
That is all... Now when you press the submit button the form should get posted to the target you specified: slips.php which is probably a script of yours... Inside that script you now can access the data like that:
$product = $_POST['product'];
$quantity = $_POST['quantity'];
There are more issue worth discussing and modifying, but as said: let's keep things simple and take one step after the other!
ChangeLog:
changed the literal key of the array element used as a value inside the radio button definition from id to procduct_id according to one of the comments below
Your radiobox need to stay inside the form.
I have made a php page. On which I am displaying table 'prod' from my data base.
each row is displayed nicely. Today i tried to add a button named 'rate' at the end of each row of my table. which I did successfully. Now I want to send the the value of the first column of that row to another php page when that button is clicked. I am stuck that how to do so? can you help please ??
I know i have to use the method post in my form and i have to use $_post[that value] on the other php page to inculcate the value for further function.
I just need to ask that where to add the value of my first column in the button line. so that onclick it can send that value. I hope I am clear over this. Thank You very much for help :)
<?php
include("connection.php");
$query = "select * from prod";
$res = oci_parse($conn,$query);
usleep(100);
if (oci_execute($res)){
usleep(100);
print "<TABLE border \"1\">";
$first = 0;
while ($row = #oci_fetch_assoc($res)){
if (!$first){
$first = 1;
print "<TR><TH>";
print implode("</TH><TH>",array_keys($row));
print "</TH></TR>\n";
}
print "<TR><TD>";
print #implode("</TD><TD>",array_values($row));
print "</TD></TR>\n";
echo "<td><form action='detailform.php' method='POST'><input type='submit' name='submit-btn' value='Rate'/></form></td></tr>";
}
print "</TABLE>";
}
?>
you have to add inputs in your form whatever kind u prefer
echo "<td>
<form action='detailform.php' method='POST'>
<input type='hidden' name='your_val_key' value='".$row[your_val_key_in_query]."'> <!-- input hidden, change to text 4 debug -->
<input type='submit' name='submit-btn' value='Rate'/>
</form>
</td></tr>";
and than, in your detailform.php u can get the val with
echo $_POST["your_val_key"];
if u are not sure how much data u send or somthing, try this and u get the full data:
echo "<pre>".print_r($_POST,true)."</pre>";
BTW: why are u mixing print and echo?
Use hidden input
echo "<td><form action='detailform.php' method='POST'><input type='hidden' name='col-name' value='you-col-value'><input type='submit' name='submit-btn' value='Rate'/></form></td></tr>";
I have a PHP code which generates a dynamic list inside a form like the following, note that the list is built dynamically from database:
echo '<form name="List" action="checkList.php" method="post">';
while($rows=mysqli_fetch_array($sql))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='hidden' name='SessionID' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}
What I need is to POST the data corresponding to the user choice when he clicks on the button for that row to another page.
If we use hyperlinks with query strings there will be no problem as I'll receive the data from the other page using a GET request and the hyperlinks would be static when showed to the user.
Also I need to obtain the user input from a textbox which is only possible with POST request.
Simply from the other page (checkList.php) I need these data for further processing:
$SessionID=$_POST['SessionID'];
$Code=$_POST['code'];
As I have a while loop that generates the fields, I always receive the last entry form the database and not the one corresponding to the line (row) that the user chosed from the LIST.
I'm going to recommend that you clean up the names of variables so that your code can
at least tell us what it's supposed to do. It should be rare that someone looks at your code
and has a lot of trouble trying to see what you're trying to accomplish :P, ESPECIALLY when you need help with something ;]. I'm going to try some things and hope that it makes doing what you want easier to comprehend and perhaps get you your answer.
It's good to try your best to not echo large amounts of HTML unnecessarily within a script , so firstly I'm going to remove the
echos from where they are not necessary.
Secondly, I'm going to use a mysql function that returns an easier to process result.
$user = mysqli_fetch_assoc($sql)
Third, I don't know if form having a name actually does anything for the backend or frontend of php, so I'm
just going to remove some of the extra crust that you have floating around that is either invalid HTML
or just doesn't add any value to what you're trying to do as you've presented it to us.
And yes, we "note" that you're building something from the database because the code looks like it does =P.
I'm also sooo sad seeing no recommendations from the other answers in regard to coding style or anything in regard to echoing html like this :(.
<?php while($user = mysqli_fetch_assoc($sql)): ?>
<form action="checkList.php" method="post">
<input type='password' name='code' value='<?php echo $user['code'] ?>' />
<input type='hidden' name='SessionID' value='<?php echo $user['id'] //Whatever you named the field that goes here ?>' />
<input type='submit' value='Take Survey' />
</form>
<?php endwhile; ?>
i not sure this is correct
echo '<form name="List" method="post">';
while($rows=mysqli_fetch_array($result))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='button' value='Take Survey' onclick=show($rows[0])>";
echo "<br>";
}
and javascript
<script>
function show(id)
{
alert(id);
window.location="checkList.php?id="+id;
}
</script>
On checkList.php
$id=$_GET['id'];
echo $id;
You can just check in checkList.php whether $_POST['code'] exists and if exists retrieve $_POST['SessionID'] which will be generated from database. But one thing, if You have all hidden fields in one form, they all will be sent, so You need to think how to correct that - maybe seperate forms for each hidden field, submit button and maybe other POST fields.
And afterwards, You will be able to get data in the way You need - $SessionID=$_POST['SessionID'];
I suppose it is the easiest way to solve that.
You can try something like this:
while($rows=mysqli_fetch_array($sql))
{
$index = 1;
echo "<input type='password' name='code' id='code'>";
//Attach $index with SessionID
echo "<input type='hidden' name='SessionID_$index' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}
On checkList.php
<?php
$num = explode('_', $_POST['SessionID']);
$num = $num[1];
//in $num you will get the number of row where you can perform action
?>
$form = 1;
while($rows=mysqli_fetch_array($sql))
{
echo '<form name="List_$form" action="checkList.php" method="post">';
echo "<input type='password' name='code' id='code_$form'>";
echo "<input type='hidden' name='SessionID' id='SessionID_$form' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
echo '</form>';
$form++;
}
it's should be something really simple if its possible. anyway of doing so? can't seem to find it online.
i have a set of checkboxes and textboxes beside it. i only want the textboxes to appear if i check it. my ids are dynamic using php so i cant use javascript out of the loop.
the code below is to test if it works to hide. i am able to hide on click but iam hoping to do the other way round.
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td><input name='CBox $TBCounter' type=\"checkbox\" value=\"{$row['type']}\" onclick=\"document.getElementById('TBox $TBCounter').style.visibility='hidden';\"/></td>";
echo "<td>" . $row['type'] . "</td>
<td><input type=\"text\" name=\"mytext\" id=\"TBox $TBCounter\"></td><br />
</tr>";
$TBCounter++;
}
You can use Javascript's window.onload to make something run every time the page loads.
Before your loop...
$elementsToHide = array();
During your loop, put this
$elementsToHide[] = $TBCounter;
Then make your script (after the loop) look like this
<script type="text/javascript">
window.onload = function ()
{
<?php foreach ($elementsToHide AS $TBCounter): ?>
document.getElementById('TBox <?php echo $TBCounter ?>').style.visibility = 'hidden';
<?php endforeach ?>
}
</script>
There are probably better (read: less intrusive) ways, but the simplest is probably to set the visibility of the element to hidden and change your Javascript to make it visible.
So, add style="visibility:hidden;" to the textbox element and change the javascript to set .style.visibility = 'visible'
Editing your code, that would be:
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td><input name='CBox $TBCounter' type=\"checkbox\" value=\"{$row['type']}\" onclick=\"document.getElementById('TBox $TBCounter').style.visibility='visible';\"/></td>";
echo "<td>" . $row['type'] . "</td>
<td><input type=\"text\" name=\"mytext\" id=\"TBox $TBCounter\" style=\"visibility:hidden\"></td><br />
</tr>";
$TBCounter++;
}
As per Joe's comment, this will not work for users with javascript disabled as the field if hidden when the page loads. One way around this is to set the field invisible in javascript by outputting an inline script tag.
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td><input name='CBox $TBCounter' type=\"checkbox\" value=\"{$row['type']}\" onclick=\"document.getElementById('TBox $TBCounter').style.visibility='visible';\"/></td>";
echo "<td>" . $row['type'] . "</td>
<td><input type=\"text\" name=\"mytext\" id=\"TBox $TBCounter\"></td><br />
</tr>
<script>document.getElementById('TBox $TBCounter').style.visibility='hidden';</script>";
$TBCounter++;
}
A third (better) alternative could be to set a class for the textboxes and use javascript to hide all elements with that class on page load.