Clear PHP echoed values input fields - php

I have a form which displays values on click from the table through the php's echo function. The echo'ing logic is as follows:
//reading input
if(isset($_POST['add'])){
$scrid=$_POST['sc'];
$desc=mysqli_real_escape_string($_POST['desc']);
$cb=mysqli_real_escape_string($_POST['created']);
$version=mysqli_real_escape_string($_POST['version']);
if($scrid !=0){
if(empty($desc)){$result="Description cannot be Empty";}
elseif(empty($cb)){$result="Created By cannot be Empty";}
elseif(empty($version)){$result="Version cannot be Empty";}
else{
echo $t."empty";
$sql="INSERT INTO testmaster(ScreenID,Description,createdBy,version) VALUES('$scrid','$desc','$cb','$version');";
if(mysqli_query($link,$sql)){
header('Location: testmaster.php');
}else{
echo "Query failed";
}
}
}
}
//passing table values,,,,, works perfectly fine...
if(mysqli_num_rows($sol)>0){
while($row=mysqli_fetch_assoc($sol)){
echo"<tr>";
echo"<td>".$row['Description']."</td>";
echo"<td>".$row['createdBy']."</td>";
echo"<td>".$row['version']."</td>";
echo "</tr>";
}
}
}
if(isset($_GET['testid'])){
$t=$_GET['testid'];
$query="SELECT * FROM testmaster WHERE tid='$t';";
if($sol=mysqli_query($link,$query)){
$modif=mysqli_fetch_assoc($sol);
$d=$modif['Description'];
$c=$modif['createdBy'];
$v=$modif['version'];
$sc=$modif['ScreenID'];
}
//These are the input fields
echo'<td><textarea name="desc" class="form-rounded" cols=50 rows=2
placeholder="Description">'.$d;
echo'</textarea></td>';
echo'<td><input type="text" name="created" class="form-control"
placeholder="Created By" value='.$c.'>';
echo'</td>';
//cancel
echo "<input type='reset' class='btn button2' name='cancel' id='cancel' value='Cancel' style='height:12px; text-align:center; margin-left:10px;' >" ;
Neither cancel button nor the javascript codes specified in Clear text field value in JQuery work.
I guess it's because I echo it.
Can anyone help me with it?
Thanks in advance.... :)
P.S. All the input fields and the cancel button are inside ...

If you've included JQuery in your code, then you can do this like that:
Add clear-onclick (or some other name) class to all fields you want empty
Add onclick handler to the button
Form code
echo'<td><textarea name="desc" class="form-rounded clear-onclick" cols=50 rows=2
placeholder="Description">'.$d;
echo'</textarea></td>';
echo'<td><input type="text" name="created" class="form-control clear-onclick"
placeholder="Created By" value='.$c.'>';
echo'</td>';
Button code
<input type='reset' class='btn button2' name='cancel' id='cancel' onclick="javascript: $('.clear-onclick').val(null)" value='Cancel' style='height:12px; text-align:center; margin-left:10px;' >
You can include jQuery as follows:
echo '<script src="https://code.jquery.com/jquery-3.3.1.js"></script>';

Related

php button call a php function

I have that code to create a button
$button = "<input type='submit' id='liga' value='liga'>";
echo $button;
I have the php function
function liga(){
....}
as I do so by clicking the button it calls the function?
Using html this code works, but I really need use the php button, how can I make it?
<input type="submit" name="liga" value="liga" />
if (isset($_REQUEST['liga'])) {
liga();
} elseif (isset($_REQUEST['desliga'])) {
desliga();
}
This little snippet will take an array with the index being the name of the button and the value as the label the button should have. It will then make a button for each element of the array.
<?php
$foo = array('name'=>'label', 'name2'=>'label2');
foreach ($foo as $k=>$v)
echo "<input type=\"submit\" name=\"$k\" value=\"$v\" />\n";
?>
You can also put the PHP variable right inside the HTML code using <?=...?>:
<input type="submit" name="<?= $myPhpVar ?>" value="<?= $myOtherPhpVar ?>" />
Or you can put complicated expressions (or whole programs) using the typical <?php...?> tags inside HTML brackets - whatever it echod becomes the content of that html tag:
<input type="submit" name="<?php echo $myPhpVar; ?>" value="<?php echo "LABEL: ".$myOtherPhpVar; ?>" />
I found a solution
echo "<input type='submit' name='liga' value='Liga'>";
echo "<input type='submit' name='desliga' value='Desliga'>";

Javascript button wont work or show

I have been trying for few hours now to try and get this working. However, the code is working fine but the second button just does not show on my website. Could you please help?
echo "<td><input class=button_normal type=button value=Google Renter onclick=window.window.open(href='https://www.google.co.uk/')";
echo "<input class=button_normal type=button value=Yahoo onclick=window.window.open(href='https://www.yahoo.co.uk')</td>";
You haven't added quotations to onclick, value and class. You also forgot to close the input tag.
echo "<td><input class='button_normal' type='button' value='Google Renter' onclick='window.window.open.href=\'https://www.google.co.uk/\''/>";
echo "<input class='button_normal' type='button' value='Yahoo' onclick='window.location.href=\'https://www.yahoo.co.uk\''/></td>";
What I suggest is this:
<script>
function goToYahoo() {
window.open('https://www.yahoo.co.uk');
}
function goToGoogle() {
window.open('https://www.google.co.uk');
}
</script>
<?php
echo "<td><input class='button_normal' type='button' value='Google Renter' onclick='goToGoogle()'/>";
echo "<input class='button_normal' type='button' value='Yahoo' onclick='goToYahoo()'/></td>";
?>
window.location is a property not method:
window.location(href='https://www.yahoo.co.uk')
Should be:
window.location.href='https://www.yahoo.co.uk'
Basically in your code you are missing input tag closing "/>"
echo <<<"FOOBAR"
<td>
<input class="button_normal" type="button" value="Google Renter" onclick="window.open('https://www.google.co.uk/')"/>;
<input class="button_normal" type="button" value="Yahoo" onclick="window.open('https://www.yahoo.co.uk')"/>
</td>
FOOBAR;

Need javascript confirm box dialogue & delete images when press ok

<?php
require "../db/dbconfig.php";
$gal=mysql_query("select * from gallery");
$numrows=mysql_num_rows($gal);
if($numrows>=1)
{
echo "<form action='delete.php' method='post' name='f2' id='f2'>";
echo '<table id="rqst" style="display:block;" border="0" cellpadding="12" cellspacing="3" width="500px">';
echo "<tr><th>Tick to select</th><th>Images in Gallery</th></tr>";
while($row=mysql_fetch_array($gal)
{
$imgfile=$row['ImgName'];
$Image="<img src=gallery/".$imgfile." width='230px' height='150px'/>";
$img_name=$imgfile;
echo "<tr>";
echo "<td><input type='checkbox' name='imgs[]' value='$img_name'></td><td>".$Image."</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td colspan='3' align='right'>";
echo "<input type='submit' value='Delete' name='del'></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
?>
This is my code....This will display images from gallery and checkboxes associated with them. When I click delete button with unchecked checkboxes an alert should come like this "Please check at least one checkbox"..How to do that??
My next problem is,,when I click delete button after checked checkbox, alert should come like this=" Do you want to delete?? "...If clicked Ok,the image must be deleted else do nothing...Please help ...Thanks in advance....
check this below link for validation using jquery:
http://jsfiddle.net/susheel61/U3Unk/2/
<form id="form">
First name: <input type="checkbox" name="firstname" class="check"><br>
<button>test</button>
</form>
$("button").on("click",function(e){
var status = $(".check").is(":checked");
e.preventDefault();
if(!status){
alert("this is not checked");
}
});
Yes, you can do it by javascript or jquery to validate whether your atleast one checkbox is select or not. So, for that you need to give a common class for all checkbox as example
<input type="checkbox" name="firstname" class="addchk">
Now in your submit button call a javascript function which validate the matter.
<input type='button' value='Delete' name='del' onclick='delete_checked()' />
Now write a function to validate whether any checkbox is selected.
function delete_checked()
{
var status = $(".addchk").is(":checked");
e.preventDefault();
if(!status){
alert("this is not checked");
}
else
{
// SUbmit yout form
}
}

Order ofQuery with AJAX PHP and MySQL

Got a bit of a confusing situation, I have a form on my webpage that allows a user to enter information into a database...
Form
<form id="insertbill">
Total <input type="text" id="total" name="total" /><br />
Bill name<input type="text" id="bill-name" name="bill-name" /><br />
bill descriptiion <input type="text" id="bill-description" name="bill-description" /><br />
bill colour<input type="text" id="bill-colour" name="bill-colour" />
<input type="button" value="submit" onClick="insertBill();" />
</form>
This form then send the information via AJAX to my php which then inputs it into my DB
AJAX
function insertBill()
{
$.post('insert_bill.php', $('#insertbill').serialize(),
function(data) {
$('#bills').append(data);
});
};
PHP
$uid = $_SESSION['oauth_id'];
$bill = mysql_real_escape_string($_POST['total']);
$billname = mysql_real_escape_string($_POST['bill-name']);
$billdescription = mysql_real_escape_string($_POST['bill-description']);
$billcolour = mysql_real_escape_string($_POST['bill-colour']);
#Insert Record
$query = mysql_query("INSERT INTO `outgoings` (user_id, bill, bill_name, bill_description, bill_colour ) VALUES ('$uid', '$bill', '$billname', '$billdescription', '$billcolour')") or die(mysql_error());
Once this is done, the data is then returned to the webpage, with another form that allows the user to update the record...
Returned data/form
Print "<tr>";
Print "<th>total:</th> <td>".$bill . "</td> ";
Print "<th>bill name:</th> <td>".$$billname . "</td> ";
Print "<th>bill deposit:</th> <td>".$billdescription . "</td> ";
Print "<th>colour:</th> <td>". $billcolour . " </td></tr>";
echo "<th>edit:</th> <td>
<form id='bill-upd'>
<input type='hidden' value='". $billname."' name='billid' id='billid''>
Total <input type='text' id='total' name='total' /><br />
Bill name<input type='text' id='bill-name' name='bill-name' /><br />
bill descriptiion <input type='text' id='bill-description' name='bill-description' /><br />
bill colour<input type='text' id='bill-colour' name='bill-colour' />
<input type='button' value='submit' onClick='updateBill();' />
</form>
</td>";
My problem is that the returned form doesn't update, this is because I need to somehow find the id of the record that was inserted, place it into my form to be returned I think... I hope this isn't too confusing, but has anybody a better way of doing this without reloading the page?
here's what what you could include on your, insert_bill.php, view page:
<script>
var json=<?
$arr=array($bill, $billname, $billdescription, $billcolour);
echo json_encode($arr); ?>;
$('tr td:nth-child(0)').html(json[0]);
$('tr td:nth-child(1)').html(json[1]);
$('tr td:nth-child(2)').html(json[2]);
$('tr td:nth-child(3)').html(json[3]);
</script>
I guess the one outstanding issue is the "id" you says gets returned from your database. Can you explain a little further? Are their multiple forms on your insert_bill.php page? Regardless, you could put that $id into the array and feed it into and html() function like I did with the other variables.
You need to return the data from the database and then change, i imagine if you just need the id you must do.
in PHP
echo mysql_last_insert_id(); // so you get the last inserted id and "return" to the JS
In JS, inside the function data
You get the echo as a return and change the id
and then $("#billid").val(data)
I hope it help you

PHP $_POST Alert

I have this form which allows the input of any product quantity from 1-10:
<form method='post' action='cart.php'>
<input type='number' name='quantitychange' size='2' min='1' max='10' value=".$_SESSION["itemsSelected"][$i][1].">
<input type='hidden' name='ProductID' value=".$_SESSION["itemsSelected"][$i][0].">
<input type='submit' value='Update'>
</form>
And another form (button) to display a selection of payment modes:
<form action='cart.php' method='post'>
<input type='hidden' name='next'>
<input type='submit' value='Select Payment Mode'>
</form>
What I want to happen is that when a user did not input anything (1st form), ex. null or 0, I want to display an alert box that says 'Product quantity can't be null or 0'.
Here's my code for that:
if (isset($_POST['next'])) {
if ($_POST['quantitychange']==null || $_POST['quantitychange']==0) {
?>
<script type='text/javascript'>
alert('Product quantity can't be null or 0.');
</script>
<?php
}
else {
echo "
//Payment modes here
";
}
}
The error is that even when a user inputs a quantity bet. 1 to 10, it still displays the alert message. Any help? Thank you.
By the way, the input type "number" only works in Google Chrome browser.
Use a small javascript (or jQuery) function to validate the form before posting it. Have this function throw up the alert if your condition isn't met and then return false. If the condition is met, return true, and it gets submitted.
Edited to add since this might get googled, I'll help a bit with code snippet I have used. The below example is jQuery and was used in production for a web application I made for my employees. document.form.doit.submit(); should be the pure javascript way of submitting the form.
<script type="text/javascript">
function subForm() {
// document.form.doit.submit();
if( test condition passes ) {
$('#save_order').submit();
}
}
</script>
<form id="save_order" action="oms_db.php" method="POST">
<input id="doit" type="button"
value="i am a button" onClick="subForm();">
</form>
I think you have some error in your forms. Instead of the below:
<input type='number' name='quantitychange' size='2' min='1' max='10' value=".$_SESSION["itemsSelected"][$i][1].">
<input type='hidden' name='ProductID' value=".$_SESSION["itemsSelected"][$i][0].">
you should be using something like this:
<input type='number' name='quantitychange' size='2' min='1' max='10' value="<?php echo $_SESSION["itemsSelected"][$i][1]; ?>">
<input type='hidden' name='ProductID' value="<?php echo $_SESSION["itemsSelected"][$i][0]; ?>">
The value parameters in the hidden input fields needs to be echoed from PHP. What you have now is like the value is simple strings ".$_SESSION["itemsSelected"][$i][0].".
I suggest you use
if(empty($_POST['quantitychange'])) { echo 'yourerror'; }
As it is far cleaner then your script. (http://php.net/manual/en/function.empty.php)
Update:
Also, you can't use two seperate forms like you do, your browser only posts whats between
<form>
</form>
Using only one will fix your problem.

Categories