Solution #1: Prompt for get input inside of anywhere code:
<?php
echo "What do you want to input? ";
$input = rtrim(fgets(STDIN));
echo "I got it:\n" . $input;
Sample output:
# php test.php
What do you want to input? Hello, I'm here!
I got it:
Hello, I'm here!
Solution #2: If you want get input in firstly inline when run php:
<?php
$input = $argv[1];
echo "I got it:\n" . $input;
Sample output:
# php test.php "Hello, I'm here!"
I got it:
Hello, I'm here!
Extra tips:
If you want to integrate CLI and WEB inputs, you can use this magic code:
<?php
if (PHP_SAPI === 'cli') parse_str(implode('&', array_slice($argv, 1)), $_GET);
var_dump($_GET);
And now write for use under CLI:
# php sample.php user=nabikaz website=nabi.ir
And follow the normal routine for the WEB:
http://localhost/sample.php?user=nabikaz&website=nabi.ir
Without defining the number and name of inputs.
Both have the same result. ;-)
array(2) {
["user"]=>
string(7) "nabikaz"
["website"]=>
string(7) "nabi.ir"
}
Answer from Nabi K.A.Z. on Stack OverflowVideos
Solution #1: Prompt for get input inside of anywhere code:
<?php
echo "What do you want to input? ";
$input = rtrim(fgets(STDIN));
echo "I got it:\n" . $input;
Sample output:
# php test.php
What do you want to input? Hello, I'm here!
I got it:
Hello, I'm here!
Solution #2: If you want get input in firstly inline when run php:
<?php
$input = $argv[1];
echo "I got it:\n" . $input;
Sample output:
# php test.php "Hello, I'm here!"
I got it:
Hello, I'm here!
Extra tips:
If you want to integrate CLI and WEB inputs, you can use this magic code:
<?php
if (PHP_SAPI === 'cli') parse_str(implode('&', array_slice($argv, 1)), $_GET);
var_dump($_GET);
And now write for use under CLI:
# php sample.php user=nabikaz website=nabi.ir
And follow the normal routine for the WEB:
http://localhost/sample.php?user=nabikaz&website=nabi.ir
Without defining the number and name of inputs.
Both have the same result. ;-)
array(2) {
["user"]=>
string(7) "nabikaz"
["website"]=>
string(7) "nabi.ir"
}
You can't take input in the middle of php execution since it finishes before the page is actually shown to the user. However, you can get input using HTML and receive that using php. Here's a really basic example:
<?php
echo $_POST['value'];
?>
<form method="post" action="">
<input type="text" name="value">
<input type="submit">
</form>
It takes the user input and reloads the page. Then, it echoes what the input was.
This is possible with a javascript onclick or onsubmit event, using GET rather than POST, but it's definitely not best practice. Use a form or AJAX, as recommended by other posters:
<input type="text" id="name" />
<button id="submit" onclick="javascript:window.location='http://yoururl.com/?name='+document.getElementById('name').value">Submit</button>
You are not able to send a form data without using <form>.
The only way to do this is to use ajax instead of a classic HTML form.
If you don't want to submit a form, the only two other ways of accomplishing this are to click a link with the data as query parameters in the url, or use AJAX to send the data to the server in the background.
update:
Javascript, as usual. You'd put a link or button somewhere on the page with "Send to Server" or whatever for the text. The script would pull your information from the input fields, and then send it on to the server via an AJAX call. Something along these lines (note that I'm using Mootools for all this, as it makes life much easier than having to do the remote calls yourself):
function clickHandler() {
var img1 = $$("input[name='img1']")[0].value;
var r = new Request.JSON({
'url: 'http://yourserver.example.com/script.php',
'method': 'post',
'onComplete': function(success) { alert('AJAX call status: ' + (success ? 'succeeded': 'failed!'); },
'onFailure': function() { alert('Could not contact server'); },
'data': 'img1=' + img1
}).send();
}
and on the server you'd have something like:
<?php
$img1 = mysql_real_escape_string($_POST['img1']);
$idx=1;
$sql="INSERT INTO games SET img1='$img1' WHERE id=$idx";
$result=mysql_query($sql);
echo (($result !== FALSE) ? 1 : 0);
You'd probably want something more complicated than this, but this is the basis of an AJAX application. Some client-side javascript that makes requests, and a script on the server that handles them and returns any data/errors as needed.
Don't know if your completely against using a form or just might not know how to keep it hidden.
You can create a hidden form that submits the info with the click of a button.
<form name="hidden-form" action="youraction.php" method="post">
<input type="hidden" name="submitme" value="I get submitted">
<input type="hidden" name="submitmetoo" value="I get submitted">
<input type="hidden" name="submitmeaswell" value="I get submitted">
<input type="hidden" name="dontleavemeout" value="I get submitted">
<input name="submit" type="submit" value="SUBMIT" />
</form>
However anyone that looks at your html will be able to see this
Instead of:
echo "<input type='submit' value='Set Images'></input><br>";
Why not:
echo "<button type='submit'>Set Images</button><br>";
You can add a GET parameter to the end of your action URL (in your case it would then be something like cm_ct_generate_preview.html?submitImage=1 or you can add a hidden input field in the form and check if that is submitted, for example:
<input type="hidden" name="submitImage" value="1" />
As your form method is already set to GET it should be sent as a GET parameter.