How to call javascript function reset() from PHP? - javascript

I am trying to call JavaScript reset() function by using button onclick method. The code is embedded in PHP as follows :
<?php
echo "<form>";
echo "<input type='text' name='keyword'>";
echo "<input type='button' value='Clear' onclick='<script>reset();</script>'>";
echo "</form>";
?>
The clear button is not working.

One solution would be to replace input button line with following:
echo "<input type='button' value='Clear' onclick='javascript:reset()'>";

try this:
<?php
echo "<form>";
echo "<input type='text' name='keyword'>";
echo "<input type='button' value='Clear' onclick='reset()'";
echo "</form>";

This should work - But i would try to get away from putting event handlers inline like this. Perhaps you could use jquery instead.
Below however is a 'Vanilla javascript" solution as you requested...
<?php
echo "
<form>
<input type='text' name='keyword'>
<input type='button' value='Clear' onclick='this.parentNode.reset()' >
</form>
";
?>

You can do this if you want :
<?php
echo "<form>";
echo "<input type='text' name='keyword'>";
echo " <input type='button' value='Clear' onclick='reset();'>";
echo "</form>";
?>
Another way to show HTML code in PHP :
Instead of using echo you can just put any HTML or Javascript code in side this ?> HTML | JAVASCRIPT CODE <?PHP
If you want to put PHP code inside that HTML code you can do the normal procedure which is this :
Example :
<?php
// Here it can be any PHP code
?>
<form>
<input type="hidden" name="<?php echo 'phpinsidehtml';?>"> //This is an example
<input type='text' name='keyword'>
<input type='button' value='Clear' onclick='reset();'>
</form>
<?php
?>

Related

can i use mail() for this code "<input type='text' name='current_user_first_name' value='<?php echo $current_user_firstname; ?>'>

I want to insert the following code in the message part of a php mail().
"<input type='text' id='current_user_first_name'
name='current_user_first_name' value='<?php echo
$current_user_firstname; ?>'>"
\ The full message will look like the following.
$to="$managers_email";
$subject="REQUEST FOR APPROVAL";
$message=
"<html>
<head>
<title>REQUEST FOR TIME SHEET APPROVAL</title>
<style>
</style>
</head>
<body>
<form>
<input type='text' id='current_user_first_name'
name='current_user_first_name' value='<?php echo
$current_user_firstname; ?>'>
<input type='submit' value='APPROVED'
formaction='http://telecomsolutions.com/approved.php'>
<input type='submit' value='DRAFT REJECT'>
<textarea name='reject_reason' placeholder='Enter reject
reason'
id='reject_reason' rows='15' cols='30'>
<input type='submit' value='REJECT'
formaction='http://telecomsolutions.com/reject.php'>
</form>
</body>
</html>";
$headers="MIME-Version: 1.0" ."\r\n";
$headers.="Content-type:text/html;charset=UTF-8"."\r\n";
$headers.='from:<$current_user_email>'."\r\n";
mail($to,$subject,$message,$headers);
echo "Your request submitted successfully"
?>
Each time a client submits a request for approval the details of the client will be fetched from the database, used in place of the variables in the html code and sent to the Manager for approval.
Please can this work and if not,any other way to make it work?
$variable = "John Doe";
$message = "Hi " . $variable . " , How are you?";
The above will result in
Hi John Dow , How are you?
You won't do like this
$variable = "John Doe";
$message = "Hi <?php echo $variable ?> , How are you?";
The first mistake you are doing is using <?php again inside your PHP file.
Then the second mistake is to use echo inside your above mentioned <php
Correct $message will be
$message=
"<html>
<head>
<title>REQUEST FOR TIME SHEET APPROVAL</title>
<style>
</style>
</head>
<body>
<form>
<input type='text' id='current_user_first_name' name='current_user_first_name' value='" . $current_user_firstname . "'>
<input type='submit' value='APPROVED' formaction='http://telecomsolutions.com/approved.php'>
<input type='submit' value='DRAFT REJECT'>
<textarea name='reject_reason' placeholder='Enter reject reason' id='reject_reason' rows='15' cols='30'>
<input type='submit' value='REJECT' formaction='http://telecomsolutions.com/reject.php'>
</form>
</body>
</html>";
The above one still looks complex and difficult to manage.
You can use concatenation (.)
$message="<html>"
. "<head>"
. "<title>REQUEST FOR TIME SHEET APPROVAL</title>"
. "<style>"
. "</style>"
. "</head>"
. "<body>"
. "<form>"
. "<input type='text' id='current_user_first_name'
name='current_user_first_name'
value='" . $current_user_firstname . "'>"
. "<input type='submit' value='APPROVED' formaction='http://telecomsolutions.com/approved.php'>"
. "<input type='submit' value='DRAFT REJECT'>"
. "<textarea name='reject_reason' placeholder='Enter reject reason' id='reject_reason' rows='15' cols='30'>"
. "<input type='submit' value='REJECT' formaction='http://telecomsolutions.com/reject.php'>"
. "</form>"
. "</body>"
. "</html>";

Order Button on each row of a table

I have a table of items and I would like to place an order button at the end of the table. The button would then take user to "order.php". Where based on the "Picture ID" they would then order the selected item. I've had a look at similar questions online however haven't been able to get it working with what I currently have.
if ($result->num_rows > 0)
{
echo "<table>\n";
while($row = $result->fetch_assoc())
{
echo "<tr>\n";
echo "<td>". $row["Picture ID"]."</td>\n";
echo "<td>". $row["name"]."</td>\n";
echo "<td>". $row["doc"]."</td>\n";
echo "<td>". $row["width"]."</td>\n";
echo "<td>". $row["height"]."</td>\n";
echo "<td>". $row["price"]."</td>\n";
echo "<td>". $row["description"]."</td>\n";
echo "<td> <form action='order.php' method='post'> <input type='button' name='id' value= ></form>"
echo "</tr>\n";
}
echo "</table>\n";
}
You have to pass the picture id as hidden input text and use the "submit" type for the button:
echo "<td> <form action='order.php' method='post'> <input type='hidden' name='id' value='" . $row["Picture ID"] . "'/> <input type='submit' name='submit' value='Buy'/></form>"
In order.php you can get the Picture ID from $_POST:
if (!empty($_POST['submit'])) { // submit button was pressed
echo 'Picture ID: ' . $_POST['id'];
}
Make sure to escape the $_POST['id'] properly when using it in SQL queries.

Submit GET form and another POST form with one button

I'm trying to submit a POST and a GET form with one submit button. I tried using the following php code:
echo "<html>\n<head>\n<title>Params</title>";
echo "<script type='text/javascript'>";
echo "function submitForms(){
document.getElementById('form1').submit();
document.getElementById('form2').submit();
}";
echo "</script>";
echo "</head>";
echo "<body>";
echo "<form action='' method='get' id='form1'>";
echo "<label>First Name </label>";
echo "<input type='text' name='first'><br/>";
echo "<label>Last Name </label>";
echo "<input type='text' name='last'><br/>";
echo "<label>Age </label>";
echo "<input type='text' name='age'><br/>";
echo "<label>Telephone </label>";
echo "<input type='text' name='phone'><br />";
echo "</form>";
echo "<form action='' method='post' id='form2'>";
echo "<label>Username </label>";
echo "<input type='text' name='username'>";
echo "<label>Password </label>";
echo "<input type='password' name='password'>";
echo "</form>";
echo "<input type='submit' value='Send' onclick='submitForms();'>";
echo "</body></html>";
What I get is only the POST params, while the GET request is not loading at all.
How can I fix this?
Thanks in advance.
You should take only one form all fields in that and after getting all input data after submit you can do whatever you want to do..
The way you are doing this is not possible reason being when form is submitted control goes to server to handle http request. hence only one form can be submit at a time. You can't submit two forms at once.. try changing order of form submit and other form will start submitting..
You should use AJAX (jQuery). Something like this should do the trick:
//Onclick for any button you wish to submit the forms
$('#form2 input[name="submit"]').click(function(e) {
e.preventDefault();
var formOne = $("#form1"),
formTwo = $("#form2");
//Post first form
$.post( formOne.attr('action') , formOne.serialize(), function() {
alert('Form one posted!');
});
//Post second form
$.post( formTwo.attr('action') , formTwo.serialize(), function() {
alert('Form two posted!');
});
});
Not tested yet, but this should work. More information for the $.post method can be found here.

handling multiple buttons with same name and value in one form

I have multiple submit buttons on a site. They used to move elements up and down and I want to be them in one whole form in case of the user changes meanwhile some other values so everything is saved. The Problem now is I need the element ID of the button which was clicked.
So here is the code in the loop :
div class="form-order"><?php echo $elements[$z]['element_order']; ?> </div>
<input type="submit" name="edit_form_operation" value="▲" class="button-primary"
<?php if($elements[$z]['element_order'] == 1) echo 'disabled="disabled"'; ?> /><br />
<input type="submit" name="edit_form_operation" value="▼" class="button-primary"
<?php
$highest_element = $fdb->get_element_highest_order($form[$i]['form_id']);
if($elements[$z]['element_order'] == $highest_element) echo 'disabled="disabled"'; ?> />
And onclick of that specific button in the for loop he should write this code first so I know which element has to be moved
echo '<input type="hidden" name="change_element_id" value="'.$elements[$z]['element_id'].'" >';
I'm also open for another solution of this problem.
Ok I solved it like that now
Here you process the input:
// Select Operation
if(isset($_POST['edit_form_operation_up'])) {
echo "move up id ";
$operation = array_keys($_POST['edit_form_operation_up']);
echo $operation['0'];
}
else if(isset($_POST['edit_form_operation_down'])) {
echo "move down id ";
$operation = array_keys($_POST['edit_form_operation_down']);
echo $operation['0'];
}
And this is in the for loop for infinite Elements the input with same value and name.
<?php $elements = $fdb->get_elements($form[$i]['form_id']);
for($z = 0; $z < sizeof($elements); $z++) {
?>
<tr>
<td><div class="form-order"><?php echo $elements[$z]['element_order']; ?> </div>
<input type="submit" name="edit_form_operation_up[<?php echo $elements[$z]['element_id']; ?>]" value="▲" class="button-primary"
<?php if($elements[$z]['element_order'] == 1) echo 'disabled="disabled"'; ?> /><br />
<input type="submit" name="edit_form_operation_down[<?php echo $elements[$z]['element_id']; ?>]" value="▼" class="button-primary"
<?php
$highest_element = $fdb->get_element_highest_order($form[$i]['form_id']);
if($elements[$z]['element_order'] == $highest_element) echo 'disabled="disabled"'; ?> />
</td>
<td><?php echo $elements[$z]['element_id']; ?> </td>
<td></td>
<td></td>
</tr>
<?php } ?>
In case of somebody finds this topic and want to find a good solution.
Greetings

Adding css and javascript to Yii's CHtml and other attributes

how do you covert this html code into the yii CHtml?
<form aciton='site/qrcode' method='POST'>
<input type='text' value='Generate Code here..' name='generate' id='gen' onclick='checkval()' class='ext'>
<input type='submit' value='Generate' id='submit'>
</form>
can anyone please help? My main aim of this is to knkow how to put a class and Onclick event in a textbox or button.
Use the third parameter of the textField method (htmlOptions array), like this:
<?php echo CHtml::beginForm(); ?>
<?php echo CHtml::textField('generate','Generate Code here...', array('id'=>'gen', 'onclick'=>'checkval()', 'class'=>'ext')) ?>
<?php echo CHtml::submitButton('Generate', array('id'=>'submit')); ?>
<?php echo CHtml::endForm(); ?>
(I left all the opening and closing tags for other html to be interspersed.)

Categories