Form Post not taking links - javascript

I got a post form and the issue is that It errors when I try to post a link like https://www.google.com/ into an imput type text, It akts as some command I do not understand, anyway heres my code and let me know what exactly is wrong.
<?php
if(isset($_POST['add'])){
$animeid = $_POST['animeid'];
$number = $_POST['number'];
$code = $_POST['code'];
echo $animeid;
echo $number;
echo $code;
}
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="600" border="1" cellspacing="1" cellpadding="2">
<tr>
<br>
<input class="inputs" placeholder="Link" name="code" type="text" id="code">
</br>
<br>
<input class="inputs" placeholder="ID" name="id" type="number" id="id">
</br>
<br>
<input class="inputs" placeholder="Number" name="number" type="number" id="number">
</br>
<br>
<center><input class="btnExample" name="add" type="submit" id="add" value="Add Episode!"></center>
</td>
</tr>
</table>
</form>
And its a simple thing really If Id to type anything other than a link it would work but I need it to be a link and thats that , I still can't see why from dosen't post links and Id like to know how can I make it post links.!
OK I think I know how to solve It But Only Know How To I will really need help on it as I did not learn javascript but There should be a way to remove the http:// or https:// before Posting The FORM And then Ill reADD it in the PhP but Now What I got To Figure It Is How Can I make a javascript that will autoremove http:// or https:// Before posting The FORM , Thanks for the Answers You Gave Me Till Now , I will apriciate If SOmeone Will Know How to autoremove words with javascript Thanks !

Well Its Simple , I Figured that Links act as A Code that wants to be posted to so what acutally happens when You Post it Its Simple Instead of Posting It To http://anime4life.net/ it Posted It To http:// linkthatIinitiallyTypedIntheInput+anime4life.net which caused to have a very long and unsucessful end
Well The Solution Is Simple , First AutoRemove The http:// straight as Im TYping Using JsQuery Like This
$('input').change( function() {
var input = $('input');
input.val(
$('input').val().replace(/https?:\/\//gi,'')
);
});
And Then That Posts The Form
Next in the echo Use This
<?PHP
echo "https://".$_POST['code']."";
And That Puts It Back For The Display , Well Thankyou All For Trying To Help!

Action is where you're sending the data to get posted (i.e. #) for same page
You need to run a set check on each variable. i.e.:
if (isset($_POST['fromPerson']){
$fromPerson=$_POST['fromPerson'];
}

change
<form method="post" action="<?php $_PHP_SELF ?>">
in
<form method="post" action="?">
and change
<?php
if(isset($_POST['add'])){
$animeid = $_POST['animeid'];
$number = $_POST['number'];
$code = $_POST['code'];
echo $animeid;
echo $number;
echo $code;
}
?>
in
<?php
echo (isset($_POST['animeid'])) ? $_POST['animeid'] : '';
echo (isset($_POST['number'])) ? $_POST['number'] : '';
echo (isset($_POST['code'])) ? $_POST['code'] : '';
?>
your code
<?php
echo (isset($_POST['animeid'])) ? $_POST['animeid'] : '';
echo (isset($_POST['number'])) ? $_POST['number'] : '';
echo (isset($_POST['code'])) ? $_POST['code'] : '';
?>
<form method="post" action="?">
<table width="600" border="1" cellspacing="1" cellpadding="2">
<tr>
<br>
<input class="inputs" placeholder="Link" name="code" type="text" id="code">
</br>
<br>
<input class="inputs" placeholder="ID" name="id" type="number" id="id">
</br>
<br>
<input class="inputs" placeholder="Number" name="number" type="number" id="number">
</br>
<br>
<center><input class="btnExample" name="add" type="submit" id="add" value="Add Episode!"></center>
</td>
</tr>
</table>
</form>

Related

Form is auto submitting but the data is not getting passed/received

I have a problem in automating a form with hidden inputs in PHP. Basically I'm doing an input for a barcode scanner where the user will input the barcode and it will auto-submit, just like in a cash registry.
The conflict which I think is the cause of the problem is because of a conditional form. Here is a snippet:
<form method="post" id="form1">
<div class="products">
<input type="text" name="code" class="form-control" autofocus required onchange="submit()" />
</div>
</form>
<?php
$query = 'SELECT * FROM product WHERE PRODUCT_CODE='.$code.' GROUP BY PRODUCT_CODE ORDER by PRODUCT_CODE ASC';
$result = mysqli_query($db, $query);
if ($result):
if(mysqli_num_rows($result)>0):
while($product = mysqli_fetch_assoc($result)):
?>
<form id="form2" method="post" action="pos.php?action=add&id=<?php echo $product['PRODUCT_CODE']; ?>" >
<input type="hidden" name="quantity" class="form-control" value="1" />
<input type="hidden" name="name" value="<?php echo $product['NAME']; ?>" />
<input type="hidden" name="price" value="<?php echo $product['PRICE']; ?>" />
<input type="submit" name="addpos" style="margin-top:5px;width: 462px" class="btn btn-info" value="Add"/>
</form>
<?php
endwhile;
endif;
endif;
?>
<script>
function submit() {
document.getElementById("form1").submit();
}
document.getElementById("form2").submit();
</script>
The data from form1 has no trouble auto-submitting, then the form2 will auto-submit but nothing happens. I need help on how can I make form2 auto-submit correctly too. I have tried different event handling for form2 but nothing happens. I only know a little bit of javascript so that's just how my script turned out.
Thank you, Programming kings!
Because the second form is inside the while loop, if there are multiple results there will be multiple forms with the same id = "form2".
You need an increment variable $incrm = 2 inside the loop,
form id='form<?php echo $incrm;?>',
with $incrm++ before ending it. I also recommend to add ann onchange event to the last input 'price' ; onchange = submit(this).
function submit(inp) {
inp.parentElement.submit();
}

How can fill the form and return back to modify the records in php

I want to fill the form and able to return back to modify the records. But when I return back I want the form to have the previous values. when I put " />
I got error.
please help
Thank you!
My code
<?php
if (isset($_POST['submit'])) {
$from = 'hello#gmail.com';
$subject = $_POST['subject'];
$text = $_POST['elvismail'];
$output_form = false;
if(empty($subject) && empty($text)) {
echo 'You forgot the email subject and body text.<br />';
$output_form = true;
}
if (empty($subject) && (!empty($text))) {
echo 'You forgot the email subject.<br />';
$output_form = true;
}
if ((!empty($subject)) && empty($text)) {
echo 'You forgot the email body text.<br />';
$output_form = true;
}
if ((!empty($subject)) && (!empty($text))) {
if(isset($_POST['cancel'])) {
echo "cancel";
}
else if(isset($_POST['send'])) {
echo "submit";
}
?>
<form method="post" action="index.php">
<table>
<tr>
<td>Subject of email</td>
<td> <?php echo $subject; ?> </td>
</tr>
<tr>
<td>Body of email</td>
<td><?php echo $text ?> </td>
</tr>
</table>
<input type="submit" name="cancel" value="cancel" />
<input type="submit" name="send" value="Submit" />
</form>
<?php
}
}
else {
$output_form=true;
}
if ($output_form) {
?>
<form method="post" action="index.php">
<label for="subject">Subject of email:</label><br />
<input id="subject" name="subject" type="text" size="30" /><br />
<label for="elvismail">Body of email:</label><br />
<textarea id="elvismail" name="elvismail" rows="8" cols="40"></textarea><br />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
}
?>
In this scenario its much better to use client side resources rather than storing it again on php. Because it is just a temporary value and using Local Storage would benefit you alot.
Javascript or JQuery would be the best approach to accomplish this.
Step 1 : Store the value on LocalStorage after on blur of every input on the form.
Step 2 : When user gets back to page. Simply assign the last data you saved on the localStorage
Because if you refresh the page you will lose the data variables in your php and will require to fetch it again on the database thus resulting on the same data values.
I suggest you read about the LocalStorage and read about controlling inputs with JQuery.
x = $("#form").serialize();
localStorage.setItem("temp_form_data" , x);
// To retrieve it simply convert it to JSON and assign it to each inputs.
var temp = JSON.parse(localStorage.getItem("temp_form_data"));
$("your-input-target").value(temp.id);
$("other-input-target").value(temp.description);
//and so on
This is just what would the implementation look like for setting and getting input data.

submitting content to a .txt using php and staying on the same page and not go to the php page

I wanna submit/write content to a .txt file and i have to used php to do it, but i don't wanna open the php page and wanna stay on the same page.
How can I do this?
Index.html
<form action="writer.php" method="POST">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
</form>
writer.php
<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "|| \n";
$ret = file_put_contents('mydata.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo " written to file";
}
}
else {
die('no post data to process');
}
?>`
You could simply use iframes, easier alternative to AJAX.
<iframe name="panel" style="display:none;"></iframe>
<form action="writer.php" method="POST" target="panel">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
</form>
...and as everyone here is yelling, consider learning AJAX.
create a php file with following in it.
<?php
if(isset($_POST['SubmitButton'])){ //check if form was submitted
$input = $_POST['inputText']; //get input text
file_put_contents('mydata.txt', $input, FILE_APPEND | LOCK_EX);
$message = "Success! You entered: ".$input;
}
?>
<html>
<body>
<form action="" method="post">
<?php if(isset($message)) echo $message; ?>
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>

Input Clear Button for Textareas

Updating what has worked and the current issue:
I have 1 clear button beside each field. Each clear button works. The problem is after clearing both fields, when selecting another file from the dropdown, it only populates the text field with the file name and does not display the contents in the second textarea until refreshing the page.
<input type="hidden" name="Action" value="EDIT" /><input type="hidden" name="Selection" id="Selection" value="-1"><div><font color=#2F6054>Below is the list of your saved codes. To view or delete your codes, select it from the list.</font></font></div><p>
<select size="1" name="CodeList" id="CodeList">
<?php
$directory = $directory = 'users/' . $_SESSION['username'];
$filesContents = Array();
$files = scandir( $directory ) ;
foreach( $files as $file )
{
if ( ! is_dir( $file ) )
{
$filesContents[$file] = file_get_contents($directory , $file);
echo "<option>" . $file . "</option>";
}
}
?>
</select><p>
<font color=#2F6054><font size=4>Enter Codes Here</font></font>
<form method="post" action="/evo/avsaveprocess.php">
<input type="hidden" name="Action" value="SAVE" />
<input type="hidden" name="CodeId" id="CodeId" value="0" />
<table width="100%">
<tr>
<td>Description:</td>
<td><input type="text" name="CodeDescription" size="40" maxlength="50" id="CodeName" value="" /></td>
<td><button type="reset" class="clear-tn" value="Clear"></button></td>
</tr>
<tr>
<td valign="top">Code:</td>
<td>
<textarea rows="10" style="width:99%" name="Code" id="CodeValue"></textarea>
</td>
</tr>
</table>
<input type="submit" value="Save" />
function code:
<script>
$(function(){
$('.clear-btn').on('click',function(e){
e.preventDefault();
$(this).closest('tr').find('input[type="text"],textarea').val('');
});
});
</script>
Edited to show current working code
Editing to show on change script at the bottom of my form for the dropdown
<script>
$(document).ready(function(){
// apply a change event
$('#CodeList').change(function() {
// update input box with the currently selected value
$('#CodeName').val($(this).val());
$.get( '<? echo $directory ?>' + '/' + $('#CodeName').val(), function( data ) {
$( "#CodeValue" ).val( data );
});
});
});
</script>
You can try the below link, it works without javascript or jquery. Just simple HTML
<form>
<input type="reset" />
<input type="submit" />
<input type="text" />
<textarea></textarea>
</form>
Here is the demo
Change type attribute "button" to "reset".
<button type="reset" class="clear-btn">Clear</button>
Try below working fiddle as per your requirement.
http://fiddle.jshell.net/t7gkr8rk/1/

Multiple forms onclick event only working for first form

I'm sure this has been asked befor but I can't seem to find it in a search
I have multiple forms on a page generated by php all with onCick event
The problem is it only picks up the first event after that any other clicks produce same result from first click
Here is javascript
function CompareScores(form)
{
var scoreA = document.getElementById("score_A").value;
var scoreB = document.getElementById("score_B").value;
if(scoreA > scoreB){
alert('Score A is Larger ' + scoreA)
}else{
alert('Score B is Larger ' + scoreB)
}
}
And the php generating forms
<?php
while($i<=$numPrelimTeams) {
if($i!=$e) {
?>
<form action="processScores.php" method="post"><p><u><?php echo $prelimTeam[$i]; ?> --> SCORE : <input type="text" class="small" id="score_A" name="score_A" size="1"></u></p>
<input type="hidden" name="team_A" value="<?php echo $prelimTeam[$i]; ?>">
<input type="hidden" name="game" value="<?php echo $game_num; ?>">
<p class="right">Game # <?php echo $game_num; ?> ) <input type="button" value="Enter Scores" onClick="CompareScores(this.form)"></p>
<?php
}else{
?>
<p><u><?php echo $prelimTeam[$i]; ?> --> SCORE : <input type="text" class="small" id="score_B" name="score_B" size="1"></u></p>
<input type="hidden" name="team_B" value="<?php echo $prelimTeam[$i]; ?>">
</form><br><br><br>
<?php
$game_num++;
$e=$e+2;
}
$i++;
}
?>
Without knowing the inputs or seeing the result, it's hard to tell for sure, but it looks like you might be generating multiple instances of this form on the same page, giving you multiple page elements named "score_A" and "score_B". document.getElementById will then become a bit ambiguous.
Since you're already sending the form object, use that instead:
var scoreA = form.score_A.value;
...
There is essentially a single problem with your code. You have multiple instances of the same ID.
To fix it, try something like this.
<input type="text" class="small score_A" name="score_A" size="1" />
Similarly
<input type="text" class="small score_B" name="score_B" size="1" />
Now, you can write a querySelector in your JS
function CompareScores(form) {
var a = form.querySelector('.score_A').value;
var b = form.querySelector('.score_B').value;
//do something
}

Categories