How to know system command is in use in php? - javascript

I have a HTML/PHP code as shown below in which on click of a button conversion of mp4 into mp3 starts happening.
HTML/PHP Code:
<?php foreach ($programs as $key => $program) { ?>
<tr data-index="<?php echo $key; ?>">
<td><input type="submit" id="go-btn" name="go-button" value="Go" data-id="<?php echo $key; ?>" ></input></td>
</tr>
<?php }?>
Php code (where mp4=>mp3 conversion happens):
$f = $mp4_files[$_POST['id']];
$parts = pathinfo($f);
switch ($parts['extension'])
{
case 'mp4' :
$filePath = $src_dir . DS . $f;
system('C:\ffmpeg\bin\ffmpeg.exe -i ' . $filePath . ' -map 0:2 -ac 1 ' . $destination_dir . DS . $parts['filename'] . '.mp3', $result);
break;
}
As soon as the button is clicked from the HTML/PHP Code above, the text gets changed from Go to Converting in the UI because I have added JS/jQuery code in my codebase but this JS/jQuery code which I have added just change the text only.
It doesn't actually know that the Conversion is happening in the background.
JS/jQuery code:
$("input[name='go-button']").click( function() {
// Change the text of the button, and disable
$(this).val("Converting").attr("disabled", "true");
});
Problem Statement:
I am wondering what modification I need to do in the JS/jQuery code above so that UI actually knows that conversion is happening in the background.
Probably, we need to add make establish some connection between JS/jQuery and php code above but I am not sure how we can do that.

Related

How to navigate to a page using base_url() function in codeigniter?

I want to navigate to a page after doing some operations. Earlier I navigated like this:
echo "<script>alert('not deleted..'); window.location='http://localhost/CodeIgniter/crud/index.php/Crud_C'</script>";
And it was working perfectly.
But now I want to navigate like this using base_url():
$this->load->helper('url');
echo "<script>alert('not deleted..'); window.location='" . <?= base_url('index.php/Crud_C'); ?> . "';</script>";
Which is not working. I have used
$config['base_url'] = 'http://localhost/CodeIgniter/crud/';
in config.php file;
What should be the correct syntax of the code after echo?
Thanks
You can use the code as below.
echo "<script>alert('Not Deleted...'); window.location.href = '" . base_url('index.php/Crud_C') . "'</script>";

PHP get information from database when echoed html code is clicked

In my code, I am retrieving data from the database using a while loop so i can have all the $FormData['fullname'] in the db.
What i want to do is, have each name display on the page and also have clickable so when someone clicks a name, it gets their user_id and pulls up information about them.
The problem i am having is that I can't figure out a way to make a it so where i can get the user_id when the user clicks a name. I tried putting a "name" in the button attribute and I checked if isset() but that didn't work.
If someone can properly figure out a way for me to basically, display all the fullnames in my database and when someone clicks a name, it pulls up information about them that is stored in the database. Here is my code
$stmtGet = $handler->prepare("SELECT * FROM formdata");
$stmtGet->execute();
while($formData = $stmtGet->fetch()){
echo "<button name='name'>$formData[fullname]</button>";
if($_SERVER['REQUEST_METHOD'] =="POST"){
if(isset($_POST['name'])){
echo "ok";
}else{
echo "bad";
}
}
}
As far i can see you are trying hit a button inside the while loop , i would not say its a bad approach , but i will suggest you not to do that . and from your code i can see you have lack of understanding post and get request learn from here . and other than this you need to know the transition of web url . how its actually works . anyway , i have given a sample code without . i hope it will help you understanding this concept.
$stmtGet = $handler->prepare("SELECT * FROM formdata");
$stmtGet->execute();
while($formData = $stmtGet->fetch(PDO::FETCH_ASSOC)){
$id = $formData['formdataid'];
echo "<a href='somepagename.php?infoid={$id}'>". $formData['fullname']."</a></br>";
}
now in the somepagename.php file or in the same page you can actually show the details information for instance
if(isset($_GET['infoid'])){
$stmt = $handler->prepare("select * from formdata where formdataid='"$_GET['infoid']"'");
$qry = $stmt->execute();
$row = $qry->fetch(PDO::FETCH_ASSOC);
echo "<p>id =".$row['formdataid']."</p></br>";
echo "<p>id =".$row['name']."</p></br>";
echo "<p>id =".$row['email']."</p></br>";
echo "<p>id =".$row['address']."</p></br>";
code is not executed , it may have semicolon or comma error warning . you have to fix those on your own . this example above shown you only the way it works .
if still you have problem ask , or see the documentation
I should stress that this is not production code and you should totally validate the data input coming in before posting queries to your DB. You can do something like this.
<?php
// Connect
$connection = mysqli_connect('localhost', 'username', 'password', 'database','port');
// Grab all users
$sql = 'SELECT * FROM users';
$users = mysqli_query($connection, $sql);
if (($_SERVER['REQUEST_METHOD'] == 'POST') && !empty($_POST['user_id'])) {
$query = "SELECT * FROM users WHERE user_id = {$_POST['user_id']};";
$user = mysqli_fetch_assoc(mysqli_query($connection, $query));
}
?>
// This only runs if our $user variable is set.
<?php if (isset($user)) : ?>
<?php foreach ($user as $key => $value) : ?>
<span><?= print_r($value) ?></span>
<?php endforeach; ?>
<?php endif; ?>
// Display all users in a dropdown and when the button is clicked
// submit it via post to this page.
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
<select name="user_id">
<?php foreach ($users as $user) : ?>
<option value="<?= $user['user_id'] ?>"><?= $user['name'] ?></option>
<?php endforeach; ?>
</select>
<button type="submit">Submit</button>
</form>
This is going to refresh your page every time. If you want to have an interactive page you are going to need to use JavaScript/AJAX to update the page elements without reloading the page. This example just demonstrates how you can achieve this with PHP and HTML.
you need to know about server-side language(php) and client-side language(javascript).
php runs before page loaded. it cannot runs when click something by itself(with ajax, it can).
most interactions without page move runs with javascript.
in your case, there are two methods.
store all information into hidden input or button's attribute. and get them by user_id via javascript.
use ajax to call another php that can select informations you need by user_id.
both use javascript or jquery. so you must learn about them.

Creating a interactive avatar selection

Similar to how XBOX LIVE or PSN allows users to select an image/avatar from their database, how would I be able to achieve the same result using Javascript/jQuery, I used PHP to achieve this functionality, however, for further learning, I'd like to explore how this functionality could be achieved through other means (js or jQuery), my PHP code is down below:
<?php
$imageFile = "../img/avatars/";
$imageLoop = glob($imageFile. '*.*' , GLOB_BRACE);
foreach($imageLoop as $image){
echo "<label id='avatarLabel'><input type='radio' class='avatarInput' name='avatar' value='$image' >
<img src='$image' id='avatarImage'></label>";
}
?>
?>
my JQuery attempt at displaying images from folder:
$(document).ready(function(){
("body").load("imgs/");
)}
There is NO way to do it using php. Php can only process the file from POST data ad it will be located in a temp directory. At that moment there will be no data about original file folder.
Might be there are ways to do it using javascript, but it will be safety violation...
Give this a try, if I understand it correctly:
<?php
$dir = '../img/avatar/';
$loop = GLOB($dir. '*{.jpg,.png}', GLOB_BRACE); // look for only jpg and png
foreach ($loop as $image) {
$filename = basename($image); // only file name without path
echo '<input type="radio" name="avatar_register" value="' . $filename . '" required><img src="' . $image . '">';
}
?>
The numeric value of $x doesn't have any meaning and I have changed to $filename so you can locate the image with
$dir . $_POST['avatar_register'];
Since you are just selecting existing images from folder, no upload process is needed. You simply need to know which image user has picked.
I also have removed the id="" as it's inside a loop. Which means multiple ids with same value will be generated. This may cause some future problem if you are trying to locate the id from DOM

Cordova PHP get back to the original page

I am building a Cordova ( intel XDk app ) with HTML CSS and JavaScript.
For Backend i use PHP. I want that after the php finishes it's job, it will return to a specific page in my app, let's say #menupage.
This is my php code:
<?php
echo 'welcome ';
echo $_GET["name"];
echo 'your password is ';
echo $_GET["password"];
echo '<button onclick="activate_page("#menupage");">Go to the menu</button>';
?>
I want the button to activate that function, but it doesn't. Every other Javascript code I try doesn't work. Can somebody help me please.
you have a wrong sequnce of quotes try assign properly and escape the inner quotes
echo '<button onclick="activate_page(\'#menupage\');">Go to the menu</button>';

Embedding multiline alert box in php code

I would like to embed a JavaScript alert box into php code and would like the text to report variable values. The following code gives multiline but does not enable me to use variables.
?>
<script type="text/javascript">
alert("Total size of all files too large.\nConsider uploading files in smaller sets.\nYou can append to existing sets.");
document.location.href="SomewhereElse.php";
</script>
<?php
The following code lets me use variables but does not give multiline.
$Str="Total size cannot be not more than " . ini_get('post_max_size');
echo "<script type='text/javascript'>\n";
echo "alert('" . $Str . "');";
echo "</script>";
When I try this code, no alert box comes up.
$Str="Unable to load (all of) these files because of their (total) size" .
"\nPlease make the upload set conform to the following parameters";
echo "<script type='text/javascript'>\n";
echo "alert('" . $Str . "');";
echo "</script>";
I do get an alert box if I leave the \n (before the Please) out but it is not multiline. I am wondering if the \n dismisses the alert box. At any rate, how do I get a multiline alert box with variables?
try something like this:
<?php
$max_size = ini_get('post_max_size');
echo '<script>';
echo 'alert("Total size cannot be more than ' . $max_size . '!\nPlease make sure...");';
echo '</script>';
?>

Categories