jquery set value on button submittion - php

problem
i have two button "YES" and "NO". if user click YES then text input will appear with value set ex: value="$check['sum'];" but when user click No then text input will appear without value ex: value ="".
here is my code
<input id="test<?php echo $row['br_loc'];?>" value="<?php echo $value;?>" name="test" type="text" class="test" style="width:70px;display:none">
and jquery
$(document).ready(function(){
$("#Yes<?php echo $row['br_loc'];?>").click(function(){
$("#test<?php echo $row['br_loc'];?>").show(1000);
});
$("#No<?php echo $row['br_loc'];?>").click(function(){
$("#test<?php echo $row['br_loc'];?>").hide(1000);
});
thanks you

Try using the disabled attribute:
$("#test<?php echo $row['br_loc'];?>").attr('disabled','disabled');
and
$("#test<?php echo $row['br_loc'];?>").removeAttr('disabled');

Try this:
$(document).ready(function(){
$("#YES").click(function(){
$("#test").show(); //value of input as set before. so, just show
}
$("#NO").click(function(){
$("#test").attr('value','').show(); //set value = '' , and show
}
});

Use binding function so that you provide your JS code with exact parameters. Code like yours should never be used.
Object ID or class should contain alphanumerical characters only, should not begin with number(s), and optionally, - or _ might be used. Avoid spaces or, as you've already shown yourself, using embedded PHP code which is highly unlikely to work in any further project of yours. It's also dangerous.

instead of this code $("#Yes<?php echo $row['br_loc'];?>")
try the following
$("#test<?php echo $row['br_loc'];?>").
Because you used id of button as "test<?php echo $row['br_loc'];?>". so only your click event not worked.

Related

Radio Input disappear on page reload (enum, foreach, default checked, remember answer on page reload)

I might be on the wrong path (tiredness..) for the moment but i have passed 4 hours or something similar to debug my code. I have a form that is auto submitted when I click on a radio button, once clicked the next form appear and let me input the customer information. But when the page reload for displaying the other form, my variable $CustomerType is set and correct and, when i complete the input form (the second one) the php check if everything in it seems correct and it does, but it says my variable $CustomerType is missing then reload the page and ask me again to set the type.
I can't paste all my code out here (~300 lines) but here is the core :
<?php $_POST['CustomerType']="SOMEONE"; ?> // Ok so this was the trick, it solved the main bug but it now fix my choice to SOMEONE only. Can't change to another type
<form method="post" action="<?php echo escaped_html($_SERVER['PHP_SELF'],'URL');?>">
<?php
$array=show_enum("customer", "CustomerType");
$checked="";
foreach($array as $CustomerType)
{
$checked="";
if(isset($_POST['CustomerType']))
{
if($_POST['CustomerType']==$CustomerType) $checked="checked";
}
echo "<input type='radio' name='CustomerType' value=".$CustomerType." ".$checked." onClick='this.form.submit()'>".$CustomerType."</input>";
}
?> </form>
EDIT Ok there is some news : by modifying the top who was : <?php $_POST['CustomerType']="SOMEONE"; ?>
TO
if(!isset($_POST['CustomerType'])) $_POST['CustomerType']="SOMEONE";
It seems to solve the second problem of the form, which couldn't let me change the type (auto-rollback to SOMEONE). But now, on form submit my choices are always rolling back to [CustomerType] => SOMEONE instead of SOMEBODY (and i checked SOMEBODY).
It means that i can't hold the value $_POST['CustomerType'] on page reload for submitting.
For example : This one which seems identical except that it's submitted with "save" button instead of onsubmit is working fine.
$array=show_enum("customer", "Language");
foreach($array as $Language)
{
$checked="";
if(isset($_POST['Language']))
{
if($_POST['Language']==$Language) $checked="checked";
}
else if($Language=="FR") $checked="checked";
echo "<input type='radio' name='Language' value=".$Language." ".$checked." />";
string2languagepic($Language);
}
Picture of the problem *OnSubmit = onClick='this.form.submit()
After looking at your code a little more I think I have spotted your problem, try the following and see if it works.
echo "<input type='radio' name='CustomerType' id='CustomerType' value='$CustomerType' $checked onClick='this.form.submit();' >"
If that fails you could always add a hidden field, and when clicking on the radio button it adds a value to it and then submits the form.

Get submit button value in PHP

I would like to test my button can get the current value from the date's textbox? I got the value of submit button but I couldn't find any code get name of the button. So what should I call I'm able to view the value?
$form_button_layout = '<button class="btn" name="oprf" value="view-log" type="submit">View</button>';
<form class="well form-inline" method="post"<?= $form_action_override; ?>>
If you'd like to get the value of 'oprf' above, then:
$value = $_POST['obrf'];
Should do it in php.
I don't think this works consitently in all browsers. Better to work with a customized name attribute.
You can try this one
if(isset($_POST['oprf'])) {
echo $_POST['oprf'];
}
Hope this helps
Add type property with submit type="submit" to button tag this will submit the form then receive the form inputs value in php script with super global variable $_POST['input name'] or $_GET[] wdepends on from action property value by default GET.
You can do:
if ($_POST['oprf'] == '' || $_POST['oprf'] == NULL) {
//this checks to see if there has been a value that was sent with the
//form
}
I usually use isset for cookies.

How to get value of edit text with jquery

Hi i want to get changed text value from JQuery but i can't select edit text with JQUERY because edit texts generated from php while loop that this php code query on database and get value of edit texts and in my program i have edit button for every edit texts and when the user changed value of edit text i select new value and when user click edit button send this value from get method to another php page with jquery $.ajax function and send new value to that php code with ajax.But i don't know how can i select edit text that it's value changed because i don't know id of that edit text!.And when i set one id for every edit text i only get first edit text value from $("#id").change().val();.I use below code but it doesn't work.I am beginner in java script and don't know how fix this problem!.
var testAnswer;
function setNewTestAnswer(id){
testAnswer = $("#id").val();
}
function sendToEdit(pID,phID,thDate,type){
var info = 'pId='+pID+'&phId='+phID+'&testAnswer='+testAnswer+'&thDate='+thDate+'&type='+type;
}
2nd function use testAnswer that user changed in edit text.
php code
<?php
include 'Connect.php';
if(match($_POST['pId'], "/^[\d]+$/") ){
$pId = $_POST['pId'];
$result = mysql_query("select pName, pID, phName, phID, testHistoryDate, type, testAnswer from patient join reception using(pID) join physician using(phID) join testHistory using(rID) join test using(tID) where pID = $pId",$connection);
}
else
die("Insert true value");
while($row=mysql_fetch_array($result)){
echo "<tr><td>";
echo $row["pName"].'</td>';
echo '<td>'.$row["phName"].'</td>';
echo '<td>'.$row["testHistoryDate"].'</td>';
echo '<td>'.$row["type"].'</td>';
$type = $row['type'];
$testHistoryDate = $row['testHistoryDate'];
?>
<td>
<span id='spryTanswer'>
<input type='text' name='tAnswer' id='tAnswer' value='<?php echo $row['testAnswer']; ?>' />
</span>
</td>
<td>
<input type='submit' value='Edit' name='edit' id='edit' onclick="sendToEdit('<?php echo $row['pID'] ?>','<?php echo $row['phID'] ?>', '<?php echo $row['testHistoryDate'] ?>', '<?php echo $row['type'] ?>')" />
</td>
</tr>
<?php } ?>
tl;dr
So it isn't completely clear what you are trying to do here but I can explain a couple things that might help.
in html ids should be unique. You dont have to obey this rule for your page to work but you have found one of the consequences if breaking it: jQuery will only find the first one.
it is a good idea to base html ids on some unique attribute of your data eg the table row id.
You can get more creative with your jQuery selectors for example
$('input[type="text"]') // gets all the text inputs
Use classes. When you want to be able to easily select all of a group of html elements you should give them all the same class name. one element can have multiple class names and many elements can share a class name. you can then select them by class name using jquery like this:
$('.myclassname')
I think you need to change your php to look more like this:
<span class='spryTanswer'>
<input type='text' name='tAnswer' id='tAnswer-<?php echo $row['id'] ?>' value='<?php echo $row['testAnswer']; ?>' />
</span>
Since you're creating elements inside a php loop, you must be sure that every element has a unique id (or no id at all). You can use either an incrementing index, or some unique value in your array. At first glance, seems that $row['pID'] is a good candidate:
<input id='edit_<?php $row['pID'] ?>' type='submit' value='Edit' ... />
After that you should be able to target individual elements.

use php to change a html elements inner text

I have a basic form, which i need to put some validation into, I have a span area and I want on pressing of the submit button, for a predefined message to show in that box if a field is empty.
Something like
if ($mytextfield = null) {
//My custom error text to appear in the spcificed #logggingerror field
}
I know i can do this with jquery (document.getElementbyId('#errorlogging').innerHTML = "Text Here"), but how can I do this with PHP?
Bit of a new thing for me with php, any help greatly appreciated :)
Thanks
You could do it it a couple of ways. You can create a $error variable. Make it so that the $error is always created (even if everything checks out OK) but it needs to be empty if there is no error, or else the value must be the error.
Do it like this:
<?php
if(isset($_POST['submit'])){
if(empty($_POST['somevar'])){
$error = "Somevar was empty!";
}
}
?>
<h2>FORM</h2>
<form method="post">
<input type="text" name="somevar" />
<?php
if(isset($error) && !empty($error)){
?>
<span class="error"><?= $error; ?></span>
<?php
}
?>
</form>
If you want change it dynamically in client-side, there is no way but ajax. PHP works at server-side and you have to use post/get requests.
Form fields sent to php in a $_REQUEST, $_GET or $_POST variables...
For validate the field param you may write like this:
if(strlen($_REQUEST['username']) < 6){
echo 'false';
}
else{
echo 'true';
}
You can't do anything client-side with PHP. You need Javascript for that. If you really need PHP (for instance to do a check to the database or something), you can use Javascript to do an Ajax call, and put the return value inside a div on the page.

JQuery/Javascript set value of form field with generated ID

On my page I have many forms, in witch field ids are generated based on DB id. like this:
<input type="hidden" name="id" value="<?php echo (isset($content))?$content->getId():''?>" />
<input type="hidden" id="x<?php echo $content->getId()?>"/>
<input type="hidden" id="y<?php echo $content->getId()?>"/>
<input type="hidden" id="x2<?php echo $content->getId()?>"/>
<input type="hidden" id="y2<?php echo $content->getId()?>"/>
That gives me unique ids for all forms.
Now I have Javascript function in jquery framework that sets vale of specified field that doesnt work:
$(function(){
$('#jcrop_target_id<?php echo $content->getId()?>').Jcrop({
onSelect: updateCoords
});
});
function updateCoords(c)
{
$('#x<?php echo $content->getId()?>').val(c.x);
$('#y<?php echo $content->getId()?>').val(c.y);
$('#x2<?php echo $content->getId()?>').val(c.x2);
$('#y2<?php echo $content->getId()?>').val(c.y2);
};
Values c.* are OK, but obviously #x<?php echo $content->getId()?> doesn't work.
What is the proper way to do it?
Regards.
Attempting to mix php and javascript can be pretty difficult. The reason for this is that they are not processed at the same time, or in the same way. PHP gets processed first on the server and the results of that process are sent to the browser. Then any javascript is processed.
Additionally, I don't think that php tags inside of javascript will actually be processed, but I'm not 100% certain about this.
One thing that you can do (but often looks pretty ugly) is to use php to actually create your javascript. For example:
echo '<script type="text/javascript">';
echo 'function updateCoords(c) {';
echo ' $(\'#x' . $content->getId() . '\').val(c.x);';
...
echo '}';
echo '</script>';
Like I said, it looks pretty ugly so I don't really recommend it, but it should actually work like you want.
I've always set the onChange event too. Does this help?
$('#jcrop_target_id<?php echo $content->getId()?>').Jcrop({
onSelect: updateCoords,
onChange: updateCoords
});

Categories