Send multiple headers or multiple submitting for the form? - javascript

When the user inserts two numbers (from-to), I want to send (((full path))) for each number.
how would I accomplish this? Is there a way in PHP, javascript or any other language. Would I send many headers or multiple submissions for the form?
P.S:
The (go.php) page that receive and process each number individually.. I can't put hands on it to make changes, I only must send individual numbers to it because that's how the other page is coded.
This what i've tried:
<form action="test.php" method="post">
<input type="text" name="first">
<input type="text" name="second">
<input type="submit" name="submit">
</form>
<?php
$f=$_POST['first'];
$s=$_POST['second'];
for($i=$f; $i<=$s; $i++){
header('location:go.php?f='.$i);
}
?>

you can not send header twice you can do this like below
<?php
$f=$_POST['first'];
$s=$_POST['second'];
header('location:go.php?first='.$i.'&second='.$s);
?>
go.php
you can catch these two variables using get method
<?php
$first=$_GET['first'];
$second=$_GET['second'];
//rest of the code
?>

Why not use javascript to submit so many times:
// supposing you have jQuery
for (var i = Number($(':input[name=first]').val()),
end = Number($(':input[name=second]').val());
i <= end; i++) {
$.get('go.php', { f: i }, function (response){
// do something with response
});
}

You can't do it via simple single script. You can do it via multithreading or curl. I am giving you example of curl:
one.php
<form action="test.php" method="post">
<input type="text" name="first">
<input type="text" name="second">
<input type="submit" name="submit">
</form>
<?php
$f=$_POST['first'];
$s=$_POST['second'];
for($i=$f; $i<=$s; $i++){
//write curl code to execute two.php with url : http:// yoursite.com/two.php?f=$i
}
?>
two.php
<?php
header('location:go.php?f='.$_REQEST['f']);
?>
Hope this explanation help you. Best of Luck.

Related

JQuery AJAX - passing data from form to PHP script

My task is triggering php script from HTML form, which is updating JSON file from which HTML is generated. Here is how i am trying to do that.
HTML Form:
<form method="post">
<label for="board">Board to update:</label><br>
<input type="text" id="board" name="board"><br><br>
<input type="button" value="Submit" onclick="update()">
</form>
Javascript:
function update() {
var board=$('#board').val();
$.post('update.php',{boardname:board}, function() {
alert("update successfull");
})
}
and PHP:
<?php
$q=$_POST['boardname'];
echo ($q);
shell_exec('curl https://cyberland.club/' . $q . '/?num=1000 > posts.json');
?>
There is no error after clicking button, but website is not updating. Can you tell me why is that? What am i doing wrong? I was using this tutorial https://www.youtube.com/watch?v=jVAaxkbmCts

How to simply post javascript data to PHP with HTML form

I have this in my javascript code:
var data = signaturePad.toDataURL('image/png');
document.write(data);
the output looks slimier to that:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAADICAYAAADGFbfiAAAHFklEQVR4Xu3VsQ0AAAjDMPr/0/yQ2exdLKTsHAECBAgQCAILGxMCBAgQIHAC4gkIECBAIAkISGIzIkCAAAEB8QMECB...
Instead of document.write(data); I want to post the data to the self page by using a simple HTML form with hidden filed that can hold the data from JS. then, I want to use PHP to handle $_POST and store the data in a variable...
<?php
// check for form submission...
if(isset($_POST['send'])) {
// get the data
$data = $_POST['data'];
}
?>
HTML form:
<form method="post">
<input type="hidden" name="data" value="">
<input type="submit" name="send" value="submit">
</form>
I understand that I can somehow do it with AJAX but I couldn't figure out with the form, how should I get this done?
I guess that I'm looking for 2 solutions - first, how to use the form with JS to store the data in the hidden field and afterwords, maybe how to post it via AJAX to PHP if it can't be just posted to the same page...
There is a lot of ways to achieve this. In regards to the way you are asking, with a hidden form element.
change your form to:
<form method="post" name="myform" method="post" action="script.php">
<input type="hidden" name="data" value="">
<input type="submit" name="send" value="submit">
</form>
Javascript:
var data = signaturePad.toDataURL('image/png');
document.myform.data.value = data;
document.forms["myform"].submit();
Or Jquery:
var data = signaturePad.toDataURL('image/png');
$('form input[name="data"]').val(data);
$("form").submit();
use like this
<?php
$data="<script>signaturePad.toDataURL('image/png')</script>";
?>
<form method="post">
<input type="hidden" name="data" value="<?php echo $data; ?>">
<input type="submit" name="send" value="submit">
</form>
works all the time.

Hide / Show form either with php or Jquery/Javascript

I have a problem here which I have solved with two different solutions. I would like to know:
Which solution is the most secure?
Which solution is more efficient, basically which one requires the least bandwith for user?
Which solution in the long run is easiest to maintain?
Is there a possibility that any of the solutions might get outdated in the near future? Example if they change how jQuery .hide() works or anything else, whatever.
Here are my solutions:
Solution 1 with jQuery and some php.
jQuery Code:
$(document).ready(function(){
$.get( "hide_forms_until_logged_in.php", function( data ) {
console.log(data.response);
if(data.response == "false") {
$("form").hide();
} else {
$("form").show();
}
}, "json");
});
HTML markup:
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="file" /> <br/>
<input type="submit" name="submit" value="Upload"/>
</form>
hide_forms_until_logged_in.php:
include 'session.php';
if (isset($_SESSION['login_user'])) {
echo json_encode(array("response"=>"true"));
} else {
echo json_encode(array("response"=>"false"));
}
Solution 2 with mostly php:
hide_forms_until_logged_in.php:
include 'session.php';
if (isset($_SESSION['login_user'])) {
echo "<form action='' method='POST'
enctype='multipart/form-data'>
<input type='file' name='file' /> <br/>
<input type='submit' name='submit' value='Ladda upp'/>
</form>";
} else {
echo "Logga in for att ladda upp filer";
}
And inside the page:
<?php include "hide_forms_until_logged_in.php" ?>
I know I could have made a function and call it inside the include there, for the sake just put the include there.
The form itself before posting anything to database has a check if the user is logged in or not.

Get value from input text in php without pressing Enter

Hay I'm new to php and I have made php code like this :
<?php
session_start();
echo 'Hellow Hisoka';
?>
<form name="form" method="post">
<div class="col-sm-3">
<label class:>Nama :</label>
</div>
<div class="col-sm-9">
<input type="text" name="nama_tamu" id='nama_tamu' class="form-control" placeholder="Nama Lengkap">
<?php
$myValue = $_POST['nama_tamu'];
?>
</div>
<br/><br/>
<?php
echo $myValue;
?>
</form>
When I want to show the echo message, I need to hit enter on my keyboard first in order to get the value of $_POST['nama_tamu'];. My question is can I get the value of nama_tamu input without pressing enter, or maybe without using POST or GET and then assign it to $myvalue?
You will need to use Javascript. You can use the Jquery events :
<script>
$( "#nama_tamu" ).keyup(function() {
alert( $this.val() );// alerting the value of the input field
});
</script>
Web development is all about communication. In this case, communication between two (2) parties, over the HTTP protocol:
The Server - This party is responsible for serving pages.
The Client - This party requests pages from the Server, and displays them to the user. In most cases, the client is a web browser.
Each side's programming, refers to code which runs at the specific machine, the server's or the client's.
You cannot get values without submitting for the user has not entered any yet. PHP is a server side language. To get values before submit and do certain actions with them you will need javascript (a client side programming language).
The simplest method to get a value is using the getElementById().
var something = document.getElementById('someid');
<input type="text" name="something" id="someid">
You can also use jQuery:
var something = $('#someid').val();
Conclusion
The simple answer to your question is: This is not possible.
Why not? I hear you asking. Because PHP doesn't know the values of your form before you send the form to your webserver.
Use keyup().
function check(id)
{
document.getElementById("result").innerHTML = id;
}
<input type="text" name="test" id="test" onkeyup="check(this.value);">
Your value: <span id="result"> </span>
$(document).ready(function() {
$("#check").keyup(function(){
alert($(this).val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="check" >
For this purpose you should use .keyup function/event. Following are the snippet :
$(document).ready(function () {
$("#nama_tamu").keyup(function(){
$("#enterdata").html($("#nama_tamu").val());
$.ajax({
type: 'POST',
url: "getdata.php",
data: "nama_tamu="+$("#nama_tamu").val(),
success: function(res)
{
$("#outputdata").html(res);
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<?php
session_start();
echo 'Hellow Hisoka';
?>
<form name="form" method="post">
<div class="col-sm-3">
<label class:>Nama :</label>
</div>
<div class="col-sm-9">
<input type="text" name="nama_tamu" id='nama_tamu' class="form-control" placeholder="Nama Lengkap">
<?php
$myValue = $_POST['nama_tamu'];
?>
<br> You press following character:<div id="enterdata"> </div>
</div>
<br/><br/>
<div id="outputdata"></div>
<?php
echo $myValue;
?>
</form>
Also create one file for the required output.
Now in getdata.php file
echo $nama_tamu=$_POST['nama_tamu'];

php variables get values from jquery variables without post to the database

<script>
$(document).ready(function(){
$("#date").click(function(){
start = $("#date_start").val();
end = $("#date_end").val();
});
});
</script>
<input name='date_A' type='text' id='date_start' />
<input name='date_B' type='text' id='date_end' />
<input type="button" id="date" class="button" value="check" />
The question is how to get value of "start" and "end" from the jquery to put that in PHP variable without post it first to database?
<?php
$start_date = "";
$end_date = "";
?>
The both variables above need the value of the variable "start" and "end" from jquery above
$(document).ready(function(){
$("#date").click(function(){
start = $("#date_start").val();
end = $("#date_end").val();
$.ajax({
type: 'post',
url: '/your_page_link/',
data: {start : start, end : end},
success: function (res) {
}
});
});
});
In PHP:
<?php
if(!empty($_POST)){ // If you are using same page, then it'll help you to detect ajax request.
$start_date = $_POST['start'];
$end_date = $_POST['end'];
}
?>
I fear you want to read values from jQuery and inject them in PHP in the same page, which is impossible.
You have to understand what PHP is first. It generates the page, that is then sent to the browser, that then executes jQuery code. PHP code completely disappeared from the generated page. jQuery has no idea of PHP's presence, they just can't interact this way. You must use a GET or POST query in order to send values to a PHP page, back to the server.
You want to read things like this : Difference between Javascript and PHP
This is what you have to do.
<form id="form" name="form" action="" method="post">
<div id="block">
<input type="text" name="startDate" id="startDate" placeholder="Start date" required/>
<input type="text" name="endDate" id="endDate" placeholder="End date" required />
<input type="submit" id="Download" name="Download" value="Download"/>
</div>
</form>
and put this code in the same page
<?php
$startDate = $_POST['startDate'];
$endDate = $_POST['endDate'];
?>

Categories

Resources