problem with enable/disable button using javascript - php

Im am trying to add a check box that will enable/disable the edit button. Im retrieving a price list and displaying it inside a table. When i add the javascript into the php code it doesn't work. Below is my code
<table border="1">
<tr>
<td width="100">Fee % </td>
<td width="100">Price</td>
<td width="100">Total</td>
<td width="102"> </td>
</tr>
<tr>
<?php
$sql1="select * from pricelist";
$result1=mysql_query($sql1) or die(mysql_error());
while ($row=mysql_fetch_array($result1)) {
$id=$row['id'];
$price=$row['h_price'];
$a=0;
print "<form id='form1' name='$a+' method='post' action=''>";
print "<td><input name='fees' value ='$fees' type='text' size='4' /></td>";
print "<td><input name='price' value ='$price' type='text' size='15' /></td>";
echo "<td><input type='checkbox' onclick='this.$a+.disabled = !this.checked;'><td>";
print"<td><input type='submit' name='$a+' value='Submit' disabled='disabled' /></td>";
print "</tr>";
print "</form>";
}
?>
</table>
Can someone please tell me what am i doing wrong?
Thanks

take out the following code out of loop... it creates multiple form tag...
print "<form id='form1' name='$a+' method='post' action=''>";
Place "<tr></tr>" inside the loop...
final code looks like:
<form id='form1' name='fm1' method='post' action=''> <tr>
<?php
$sql1="select * from pricelist";
$result1=mysql_query($sql1) or die(mysql_error());
while ($row=mysql_fetch_array($result1)) {
$id=$row['id'];
$price=$row['h_price'];
$a=0;
print "<tr>";
print "<td><input name='fees' value ='$fees' type='text' size='4' /></td>";
print "<td><input name='price' value ='$price' type='text' size='15' /></td>";
echo "<td><input type='checkbox' onclick='this.$a+.disabled = !this.checked;'><td>";
print"<td><input type='submit' name='$a+' value='Submit' disabled='disabled' /></td>";
print "</tr>";
}
print "</form>";
?>
Next if u want to control the button only then
<input type='checkbox' onclick='document.$a.submit.disabled = !this.checked;' />
and make sure of the following things:
1.) form name should be $a
i.e <form name='$a' ...>
2.) submit buttons name should be submit
i.e <input type='submit' name='submit'...>
3.) increase the $a variable only at the end of loop
i.e
$a++;
}

i think $a+ will cause syntax error. you need to increment $a at the end of loop like $a++. Also see page source and see what is coming from server.

I would recommend using following format for html + php output
<form id='form1' name='<?=$a++?>' method='post' action=''>
<tr>
<td><input name='fees' value ='<?=$fees?>' type='text' size='4' /></td>
<td><input name='price' value ='<?=$price?>' type='text' size='15' /></td>
<td><input type='checkbox' onclick='this.disabled = !this.checked;'><td>
<td><input type='submit' name='<?=$a++?>' value='Submit' disabled='disabled' /></td>
</tr>
</form>
<?
you also need to have < tr > to be in the while loop.

In your html part where you set up the formular you have to use " " to escape the php code. example:
print "<form id='form1' name='".$a+."' method='post' action=''>";
But as Adeel said already, what is $a+ ?
And you have to edit all prints and echoes in the way, that php expressions are excaped from the HTML source.

Related

change to php 8 some POST data after HTML form submit is empty [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 days ago.
I pass data from a form (once hidden and once by input) by click to a function. But unfortunately the data on the hidden input field is given to the $_POST
<?php>
$abschlag1 = "gelb";
$ndbw01 = 8;
echo "<form name='Formular' action='' method='post' >";
echo "<td align=center><input type='text' pattern='\d*' maxlength='2' id='L01w' name='L01w' size='2'>" . $ndbw01 . "</td>";
echo "<input type='hidden' id='abschlag1' name='abschlag1' value=$abschlag1>";
echo "<input type='submit' name='submit1' value='Speichern' onclick='chkFormular();'/>";
echo "</form>";
?>
<script type="text/javascript">
function chkFormular() {
<?php
if(isset($_POST['submit1'])) {
$abschlag1 = $_POST['abschlag1'];
$L01w = $_POST['L01w'];
}
?>
}
</script>
The result ist:
$_POST['abschlag1‘] => gelb
$_POST['L01w‘] => empty
I hope, it is now a little clearer.
You can't run a PHP script on a JavaScript event directly. PHP usually is executed at page generation on the server side but if really needed you can run a PHP script on a JavaScript event using Ajax for example.
In order to help you, correct me if I'm wrong, but I think what you want to do is having your fields still completed with the sent values after the form is submitted. You can do it like this using:
value='<?php if isset($_POST["field"]) echo $_POST["field"]; ?>'
<form name='Formular' action='' method='post'>
<td align='center'>
<input type='text' pattern='\d*' maxlength='2' id='L01w' name='L01w' size='2' value='<?php if isset($_POST['L01w']) echo $_POST['L01w']; ?>' /><?= $ndbw01; ?>
</td>
<input type='hidden' id='abschlag1' name='abschlag1' value='<?php if isset($_POST['abschlag1']) echo $_POST['abschlag1']; ?>' />
<input type='submit' name='submit1' value='Speichern' onclick='chkFormular()' />
</form>
If you still want to keep your data in variables, you update your code to this:
<?php
if(isset($_POST['submit1'])) {
$abschlag1 = $_POST['abschlag1'];
$L01w = $_POST['L01w'];
}
?>
<form name='Formular' action='' method='post'>
<td align='center'>
<input type='text' pattern='\d*' maxlength='2' id='L01w' name='L01w' size='2' value='<?= $L01w; ?>' /><?= $ndbw01; ?>
</td>
<input type='hidden' id='abschlag1' name='abschlag1' value='<?= $abschlag1; ?>' />
<input type='submit' name='submit1' value='Speichern' onclick='chkFormular()' />
</form>

How can I use PHP variables inside HTML tags?

I have the following doubt. I want to use php code to create a HTML form which outputs three values. And I want to use that values on a php function, something alike the following:
<?php
function modcreate(){
echo "<input type='text' name='dato'>";
echo "<input type='text' name='nvalor'>";
echo "<input type='text' name='identif'>";
echo "<input type='button' onclick=mod($GET['dato'], $GET['nvalor'],
$GET['identif']>";
}
?>
Any suggestions or cleaner ways to do this? Thank you beforehand
Try this one
<?php
function modcreate(){
echo "<input type='text' name='dato'>";
echo "<input type='text' name='nvalor'>";
echo "<input type='text' name='identif'>";
echo "<input type='button' onclick=" . mod($GET['dato'], $GET['nvalor'],
$GET['identif'] . ">";
}
?>
You were actually inserting php code inside double quotes which was behaving like a simple string. So close your double quotes before adding php tag
you dont need to echo html in php
function modcreate(){
?>
<input type='text' name='dato'>
<input type='text' name='nvalor'>
<input type='text' name='identif'>
<input type='button' onclick="mod(<?php (implode(',',[$GET['dato'],$GET['nvalor'],$GET['identif']);?>)">;
<?php
}
For a better syntax of how to handle html without echo is:
<?php
function modcreate(){ ?>
<input type='text' name='dato'>
<input type='text' name='nvalor'>
<input type='text' name='identif'>
<input type='button'
onclick="mod(
<?php (implode(',',[$GET['dato'],$GET['nvalor'],$GET['identif']); ?>
)">
<?php } ?>

How do I update each row individually?

So I've been trying for days to figure out how I can update each row separately and i can't figure it out. this is the first time I've used php so sorry for the ugly code.
I can seem to pull the information from each row but the update wont work, however it sends all the data from all the rows in the address bar
<html>
<head>
<title>EDIT Patient data</title>
</head>
<body>
<br>
Edit Patients<br>
Patient Request<br>
Booked Patients
<?php
include 'connect.php';
$sql = "SELECT * FROM patients;";
$result = mysql_query($sql);
$num_of_row = mysql_num_rows($result);
if (isset($_GET['submit'])) {
$id = $_GET['did'];
$fname = $_GET['dfname'];
$lname = $_GET['dlname'];
$email = $_GET['demail'];
$phone = $_GET['dphone'];
$address = $_GET['daddress'];
$query = mysql_query("update patients set fName='$fname', lName='$lname', email='email', phone='$phone', address='$address' where patientID ='$id'");
}
echo "<table border=1>";
echo " <tr> <td> Patient ID </td> ";
echo "<td> First name </td>";
echo "<td> last name </td>";
echo "<td> email </td>";
echo "<td> phone number</td>";
echo "<td> address </td>";
echo "<td> date of birth </td> ";
echo "<td> update </td></tr>";
while($row = mysql_fetch_assoc($result))
{
echo "<tr> <form class='form' method='get'>";
echo "<td> <input type='text' name='did' value='".$row['patientID']."' readonly /></td>";
echo "<td> <input type='text' name='dfname' value ='".$row['fName']."' /></td>";
echo "<td> <input type='text' name='dlname' value = '".$row['lName']."' /></td>";
echo "<td> <input type='text' name='demail' value = '".$row['email']."' /></td>";
echo "<td> <input type='text' name='dphone' value ='".$row['phone']."' /></td>";
echo "<td> <input type='text' name='daddress' value ='".$row['address']."' /></td>";
echo "<td> <input type='text' name='ddob' value ='".$row['dob']."' readonly /></td>";
echo "<td> <input class='submit' type='submit' name='submit' value='update' /> </td> </tr>";
}
?>
</body>
</html>
Does your user have update permissions?
If not you need to use GRANT UPDATE ON *.* TO 'User'#'Host';
But you should use MySQLi as MySQL is deprecated. You should also look into Parameterized Queries and MySQL injection, It's worth knowing about!
If you want to update an individual row, you'll have to put a <form> around your while-loop, like this:
echo "<form action='' method='GET'>";
while ($row = mysql_fetch_assoc($result)) {
echo "<tr> <form class='form' method='get'>";
echo "<td> <input type='text' name='did' value='".$row['patientID']."' readonly /></td>";
echo "<td> <input type='text' name='dfname' value ='".$row['fName']."' /></td>";
echo "<td> <input type='text' name='dlname' value = '".$row['lName']."' /></td>";
echo "<td> <input type='text' name='demail' value = '".$row['email']."' /></td>";
echo "<td> <input type='text' name='dphone' value = '".$row['phone']."' /></td>";
echo "<td> <input type='text' name='daddress' value ='".$row['address']."' /></td>";
echo "<td> <input type='text' name='ddob' value ='".$row['dob']."' readonly /></td>";
echo "<td> <input class='submit' type='submit' name='submit' value='update' /> </td> </tr>";
}
echo "</form>";
The form will allow input fields to be submit according to the form's method (GET and POST).

Input form wont submit?

protected function pro_show_sendRequestForm(){
echo("<SECTION id='page_content-small'>");
echo("<ARTICLE>");
echo("<table width='100%' cellspacing='5'>");
echo("<tr><td><form method='post' action='main.php'></td></tr>");
echo("<tr><td colspan ='2' align='center'><B><H2>Send a event form.</H2></B></td></tr>");
echo("<tr><td align='left'><B>Name event:</B></td>");
echo("<td align='left'><input name='sEventTitle' type='text' size='30' maxlength='50' required></td></tr>");
echo("<tr><td align='left'><B>Name organisation:</B></td>");
echo("<td align='left'><B>New: </B><input name='sNewOrganisationName' type='text' size='30' maxlength='50'></td></tr>");
echo("<tr><td> </td><td align='left'><B>Or choose: </B>");
$this->pro_OrganisationDropdown('sExcistingOrganisationRecordNumber');
echo("</td><tr>");
echo("<tr><td align='left'><B>E-mail address:</B></td><td align='left'>");
echo("<input name='sEmailadress' type='text' size='30' maxlength='50' required></td></tr>");
echo("<input type='hidden' name='sSubmitForm' value='SendIntakeForm'>");
echo("<tr><td colspan='3' align='center'><input type='submit' value=' Sending '></td></tr>");
echo("</form></table></ARTICLE></SECTION>");
}
Hi you all,
Today I created this form and it worked fine. Until I started with testing and then i discovered that a certain value in the input field 'sEventTitle' was the reason that the form wasn't processed. I checked and rechecked but the value for example 'Apple' was excepted and the value 'pineapple' wasn't.
So to discover why, I commented all other input fields out like this:
protected function pro_show_sendRequestForm(){
echo("<SECTION id='page_content-small'>");
echo("<ARTICLE>");
echo("<table width='100%' cellspacing='5'>");
echo("<tr><td><form method='post' action='main.php'></td></tr>");
echo("<tr><td colspan ='2' align='center'><B><H2>Send a event form.</H2></B></td></tr>");
echo("<tr><td align='left'><B>Name event:</B></td>");
echo("<td align='left'><input name='sEventTitle' type='text' size='30' maxlength='50' required></td></tr>");
//echo("<tr><td align='left'><B>Name organisation:</B></td>");
//echo("<td align='left'><B>New: </B><input name='sNewOrganisationName' type='text' size='30' maxlength='50'></td></tr>");
//echo("<tr><td> </td><td align='left'><B>Or choose: </B>");
//$this->pro_OrganisationDropdown('sExcistingOrganisationRecordNumber');
//echo("</td><tr>");
//echo("<tr><td align='left'><B>E-mail address:</B></td><td align='left'>");
//echo("<input name='sEmailadress' type='text' size='30' maxlength='50' required></td></tr>");
echo("<input type='hidden' name='sSubmitForm' value='SendIntakeForm'>");
echo("<tr><td colspan='3' align='center'><input type='submit' value=' Sending '></td></tr>");
echo("</form></table></ARTICLE></SECTION>");
}
Now the entire form wont submit itself. This can not be true.
The basics are there: form and /form. A input name ='foo' type='text' and a submit button. Why is this form not submitted when the larger one is working? What is my mistake? Is there something I don't see? Any help is appriciated.
The first lines from main.php to see if the form is submitted are:
$oMainPage = new main();
echo($_POST['sEventTitle']);
echo($_POST['sSubmitForm']);
The code generated from the browser:
<SECTION id='page_content-small'><ARTICLE><form method='post' action='main.php'>
<table width='100%' cellspacing='5'><tr><td colspan ='2' align='center'><B>
<H2>Een aanmeldformulier versturen.</H2></B></td></tr><tr><td align='left'>
<B>Naam evenement:</B></td><td align='left'>
<input name='sEventTitle' type='text' size='30' maxlength='50' required></td></tr>
<input type='hidden' name='sSubmitForm' value='SendIntakeForm'><tr>
<td colspan='3' align='center'><input type='submit' value=' Versturen '></td></tr>
</table></form></ARTICLE></SECTION>
take the form out of the <td>, wrap the <form> around that <table>

form submit not working in firefox , works fine in chrome

I am fetching data from database , each rowof data creates a form , each row has a "book" button, which submits phase and site data, but this "book" button does not work in Firefox and IE , it works in chrome
<?php
echo"<div style='overflow-y:scroll;height:200px;float:left;' ><table border=1 >
<tr>
<td>phase</td> <td>site no.</td> <td>plot-size</td> <td>face</td> <td>sply</td> <td>status</td><td>select </td>
</tr>" ;
while($row = mysql_fetch_array($ret, MYSQL_ASSOC))
{
echo "<tr>".
"<form action='restricted.php' method='get'>".
"<td><input type='text' value=\"{$row['phase']}\" name='phase' size='3' readonly /> </td>".
"<td><input type='text' value=\"{$row['id']}\" name='site' size='4' readonly /></td>".
"<td> {$row['size']} </td>".
"<td> {$row['facing']} </td>".
"<td>{$row['sply']} </td> ".
"<td>{$row['status']} </td> ".
"<td><input type='submit' name='book' value='book' \" /></td>".
"</form>".
"</tr>";
}
echo "</table></div>";
?>
you cannot have a form within a <tr> element. You must either put the form outside the table, or inside a <td> element. Firefox is probably complaining about that.

Categories